开发者

return TCollection or array of objects from Dll

I tried to return from dll function my own object (derived from TCollection). I used FastMemoryManager, but without success... So I tried to return dynamic array of some objects.

Length of the array of set of course in dll function. It works realtive good, but allocated memory is not freed.

(I measure with Windows tarsk manager). Is there some possibility how to release the dynamic array? The procedure which call the dll function is in the thread, and in the end I have follows:

for i := 0 to length(MyObjectArray) - 1 do begin
  if MyObjectArray[i] <> nil then
     MyObjectArr开发者_开发问答ay[i].Free;
end;
Setlength(MyObjectArray, 0);
MyObjectArray := nil;

If I used instead of Setlength(MyObjectArray, 0) and MyObjectArray := nil,

FreeAndNil(MyObjectArray) exception was raised.

Any suggestion?


Is ShareMem the first unit in all Delphi DLL and EXE project files? FastMM is already the RTL's memory manager for the past few releases of Delphi.

I would recommend not sharing objects at all between DLLs and EXEs; it's just a recipe for pain. Use packages instead.

If you must use DLLs, I'd advise adopting the usual WinAPI conventions: stdcall calling convention, only using C-compatible data types (integers, floats, pointers, records that have no fields of managed types like strings, arrays or interfaces). Have the DLL do no allocation of memory that the EXE is responsible for freeing. Instead, let the EXE allocate and pass the DLL the memory; alternatively, encapsulate allocations into logical handles, and export functions that dispose of the memory from the DLL, along the lines of e.g. how the CloseHandle WinAPI function works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜