views:

92

answers:

1

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?

+3  A: 

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.

TOndrej
So, is it wrong to have a unit `CommonStuff` both in the packages `contains` clause and in the application? What's the alternative? Not sure if I fully understand you answer. Maybe you can explain it in a bit more details. Thanks!
Smasher
It's OK to use the unit in the application, but the project has to use the runtime package which contains it, otherwise you're linking the unit again in the executable. If you're using more packages, each unit has to be only in one package (contains clause). Any other package using the same unit must reference the original package (requires clause).
TOndrej
Thanks for your help! I got it working now. +1 and accepted.
Smasher