[comp.windows.ms.programmer] Global Memory Goes Away?

kevin@msa3b.UUCP (Kevin P. Kleinfelter) (05/09/91)

I allocate some memory with "GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,2048)"
in instance 1 of my program.  I store the handle to this memory in a
class extra word.  Instance 2 shares this memory, by getting the handle from
the class word.

This works fine, until instance 1 terminates; at this point instance 2
can no longer GlobalLock.  It looks like Windows is calling GlobalFree
for me.  Other than writing a DDE for sharable memory, how can I prevent the
freeing and allow sharing?

-- 
Kevin Kleinfelter @ DBS, Inc (404) 239-2347   ...gatech!nanoVX!msa3b!kevin
English Lesson: THEY'RE at THEIR home, over THERE. YOU'RE sure of YOUR facts?
"Its" & "their" are like 'his'. "They're" == "they are." "It's" == "it is."
If you can do regular expressions, you can handle a natural language. Syntax!

venkat@tandoori.East.Sun.COM (Desikan Venkatrangan - Sun BOS Software CONTRACTOR) (05/13/91)

In article <1639@msa3b.UUCP> kevin@msa3b.UUCP (Kevin P. Kleinfelter) writes:
>I allocate some memory with "GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,2048)"
>in instance 1 of my program.  I store the handle to this memory in a
>class extra word.  Instance 2 shares this memory, by getting the handle from
>the class word.
>
>This works fine, until instance 1 terminates; at this point instance 2
>can no longer GlobalLock.  It looks like Windows is calling GlobalFree
>for me.  Other than writing a DDE for sharable memory, how can I prevent the
>freeing and allow sharing?
>

The Windows SDK Guide to Programming, Section 16.5 "Traps to Avoid Managing
Program Data" specifically says that "You may not use a global handle to
share data with another application".  It also says that "The only methods
supported by Windows to pass data between applications are the clipboard
and the DDE protocol".

There are many more problems you have not run into yet, some of which
are explained in that section.

Thomas_Hornschuh@p1.f36.n245.z2.fidonet.org (Thomas Hornschuh) (05/15/91)

KP> I allocate some memory with
KP> "GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,2048)"
KP> in instance 1 of my program.  I store the handle to this memory in a
KP> class extra word.  Instance 2 shares this memory, by getting the handle
KP> from
KP> the class word.
KP> 
KP> This works fine, until instance 1 terminates; at this point instance 2
KP> can no longer GlobalLock.  It looks like Windows is calling GlobalFree
KP> for me.  Other than writing a DDE for sharable memory, how can I prevent
KP> the
KP> freeing and allow sharing?

Windows discards (frees) all memory allocated by a task when the task 
terminates. The only way to allocate a "permanent" memoryblock is, wrting a 
special task that allocates and holds the sharable memory. You should 
communicate with this task over DDE-messages.