3

I am calling a C DLL from a Delphi 2009 application and I keep getting errors when memory allocated by GetMem or AllocMem is passed to the DLL. The only way around I could avoid these errors was by using malloc from msvcrt.dll. What is malloc doing that the built-in memory routines aren't, and how can I get the built-in ones to work? I really don't like bypassing the built-in memory manager.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 1
    What sort of errors are you getting? There shouldn't be any problems unless the C dll tries to free or realloc the memory you passed to it, which it really shouldn't be doing. – Mason Wheeler Nov 10 '09 at 16:02
  • Agreed, but unfortunately "shouldn't be" and "isn't" are different things, and far too frequently so. – qid Nov 10 '09 at 16:05
  • 1
    Thank you for the answer. The dll has an option to take control of the memory and I was using it. I will rewrite my function to keep control of the memory. –  Nov 10 '09 at 16:07

2 Answers2

4

If the DLL ever attempts to free that memory or otherwise manipulate the memory allocation (e.g. expand/contract it), that would explain it. Mixing memory allocation systems is not recommended.

qid
  • 1,883
  • 10
  • 15
0

Pay attention to Calling Convention, stdcall or cdecl .

Pham
  • 51
  • 2