[fpc-pascal] How to free memory allocated by C++ library
jonas.maebe at elis.ugent.be
Thu Jun 2 15:02:12 CEST 2011
On 02 Jun 2011, at 14:27, Henry Vermaak wrote:
> On 02/06/11 13:04, Jonas Maebe wrote:
>> On 02 Jun 2011, at 10:35, Henry Vermaak wrote:
>>> If you add cmem to the start of your uses list, all the memory operations are routed through the c memory manager. You should then be able to use Free safely for pointers that were allocated by your library.
>> That is incorrect. The cmem unit adds extra size information to all allocated memory blocks (for so that memsize() works). Additionally, most C++ memory allocations happen via new/delete, and using libc's "free" to free a memory block allocated by "new" is almost guaranteed to cause crashes down the line.
> So it works in C, but not C++? Or have I completely misunderstood the documentation?
You will not be able to use it to free memory allocated in either C or C++ programs, unless all they do is use malloc *and* after every allocation increase the pointer with sizeof(ptruint). And even that's just an implementation detail on which you cannot rely.
The main use of cmem is to offer the same functionality as the sharemem unit.
More information about the fpc-pascal