views:

161

answers:

2

This question is a follow up of this question.

How can I achieve that my application and my runtime package use the same memory manager? I thought that this was the case by default but since I get strange access violations and invalid pointer exceptions, I inserted calls to GetMemoryManagerState just before entry to the runtime package and in the runtime package and the returned state seems to indicate that the package and the application use separate memory managers.

Before calling a method in the runtime package, there are 61 allocated medium blocks and after the call (at the beginning of the called method inside the package) there are 0 allocated medium blocks.

Do you happen to have an explanation for that? What do I have to do to make package and application share the memory manager? Any more complicated issues when using FastMM?

EDIT BTW, is there a better way to check if the same memory manager is used than comparing memory manager state? TMemoryManager turns out to be a record, so I can't compare adresses.

+4  A: 

A package uses the same RTL state as the main program. That is part of the definition of package.

Memorymanager inclusive.

Btw, my notes about packages are here: http://wiki.freepascal.org/packages

Currently FPC is still quite far away from this (mainly due to the need to implement this on umpteen OSes and architectures through an external linker). Also due to the larger number of FPC versions in roulation, I suspect it will be less useful than many people think (I don't expect Lazarus components to be ever distributed in something as binary only FPC equivalent of BPLs/.dcp combos)

Marco van de Voort
Yes, assuming the program is built with runtime packages and references rtl in project options, and the package itself requires rtl.dcp. That's the case by default but it can be changed.
TOndrej
Interesting point TOndrej. Didn't know that was possible. Thanks
Marco van de Voort
But @Tondrej, if you do all that, haven't you pretty much broken everything else in your program, too? I think Smasher has demonstrated exactly that.
Rob Kennedy
@Rob, yes, very likely. But - with a bit of care - the possibility is there to "rebundle" Delphi units in your own packages and use them instead of the supplied rtl, vcl, etc. Perhaps useful in some corner cases, or if you want to use modified RTL/VCL units.
TOndrej
+1 Thanks for making this clear.
Smasher
TOndrej: in how far does it being a package matter than compared to a normal DLL? Since neither program state, nor VMTs are shared.
Marco van de Voort
Have a look at a bpl in Dependency Viewer, or tdump. A package exposes RTTI, interfaces, classes/VMTs of its contained units as exported functions. This is the mechanism the compiler/linker uses when you build with runtime packages and what makes the code shared in the host process.
TOndrej
+1  A: 

Thanks to ldsandon and Rob Kennedy I figured out that it is necessary to use runtime packages (at least for the RTL) in order for the application and the package to share memory managers.

Enabling "Use runtime packages" and adding "rtl" to the package list solved the problem. Thanks to you all for your help.

Smasher