Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2005 May : can host "hold on" to DLL's HeapAlloc?

www.cryer.info
Managed Newsgroup Archive

can host "hold on" to DLL's HeapAlloc?

Subject:can host "hold on" to DLL's HeapAlloc?
Posted by:"Kevin Killion" (kevinzzz..@shsmedia.com)
Date:Wed, 25 May 2005 14:08:48

What happens to memory declared by a DLL between calls to the DLL's
routines?

Here's what I've got now, and it works just peachy in a single-user
basic app.  But it crashes badly when used in a complex application.

Do you see anything wrong with this (stripped to the basics):

   -- A host Delphi program calls a DLL which returns a "ref":

          StartMeUp(ref);

   -- The DLL reserves some memory using HeapAlloc
      and returns a "reference" to the host, like this:

          procedure StartMeUp(var ref: longint);
          ...
          begin
              c := HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, 1000);
              ref := LONGINT(c);
          end;

    -- Then periodically, the host makes additional
       calls, using that "reference", which lets the
       DLL access that memory again:

           DoSomething(ref, other_args);

    -- Eventually, the host cleans up with a third
       call to the DLL:

           CleanUp(ref);

    -- The DLL then disposes of this memory:

           procedure CleanUp(var ref: longint);
           begin
               HeapFree(GetProcessHeap, 0, ref);
               ref := 0;
           end;

Note that the DLL does not itself maintain that "reference," not even in
a global somewhere.  It's up to the host to receive it, and pass it back.

As I said, it works fine now in a straightforward app, but I'm wondering
if there is anything evil here when the thing is used in a complex
situation.

THANKS!!!!!

Kevin Killion

Replies:

www.cryer.info
Managed Newsgroup Archive