jpfollenius
jpfollenius

Reputation: 16620

Package Memory Management

Since I have been running in a lot of difficulties when trying to use DLLs, I decided to try out runtime packages (mainly to avoid the memory manager and type registry problems).

From my application I do something like this:

HandleList := TList <THandle>.Create;
try
  PackageObj.DoSomething (HandleList);
finally
  FreeAndNil (HandleList);
end;

The method (inside the runtime package) just adds something to the list:

procedure TPackageObject.DoSomething (HandleList: TList <THandle>);
begin
  HandleList.Clear;
  HandleList.Add (0);
end;

I do get Invalid Pointer exception either in the call to Clear within the package or in the call to FreeAndNil in the application. Access violations also happen from time to time.

When using FastMM, it sometimes reports "Block Header has been corrupted".

The error always happens when memory is allocated or freed, i.e. adding something to the list and therefore causing the list to grow dynamically.

Is the way the HandleList object is passed to the package and back okay? Is there anything important to know about packages and memory management? Or must the error be somewhere else?

EDIT In the case that the error is likely to be somewhere else, how am I supposed to debug something like this? Any experiences?

Upvotes: 0

Views: 302

Answers (1)

Ondrej Kelle
Ondrej Kelle

Reputation: 37221

I guess you are compiling the same code in different packages, maybe in the application executable, too. Whether you link your runtime packages statically (list them in project options) or dynamically (using LoadPackage), each unit has to be linked into only one module, otherwise you get duplicate code conflicts. Check your package dependencies (requires clauses), list of runtime packages in the main project options, watch compiler warnings about implicitly included units.

Upvotes: 3

Related Questions