Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

LocalAlloc() & LocalFree() GPF

0 views
Skip to first unread message

d...@tntvax.ntrs.com

unread,
Sep 8, 1994, 7:53:36 PM9/8/94
to
Below is an example of a function I have written that causes a GPF, and I am
unsure of how to correct it. The function is part of a DLL with a heap size of
4K - more than enough for the function.

Question is at the end...

---------------------------------------------------------

int FAR PASCAL fnExample()

{

HLOCAL hloc1;
HLOCAL hloc2;
HLOCAL hloc3;

char *pchString1;
char *pchString2;
char *pchString3;


hloc3 = LocalAlloc(LPTR, 100);
pchString3 = (char *) LocalLock(hloc3);

hloc2 = LocalAlloc(LPTR, 50);
pchString2 = (char *) LocalLock(hloc2);

hloc1 = LocalAlloc(LPTR, 110);
pchString1 = (char *) LocalLock(hloc1);

// some strcopies.... cats and _lwrites...

LocalUnlock(hloc1);
LocalUnlock(hloc2);
LocalUnlock(hloc3);
LocalFree(hloc3); // <<< GPF
LocalFree(hloc2);
LocalFree(hloc1);

return(TRUE);
}


The GPF occurs when the function is called for a second time, and the
LocalFree(hloc3) is performed. That is:

RC = fnExample(); OK
RC = fnExample(); GPF on LocalFree(hloc3)

After tracing the function I found the hloc memory handle values were the same
except for one... example values:

First Call Second Call

hloc1 1000 1500
hloc2 2000 2000
hloc3 3000 3000


IF I swap the values of hloc3 and hloc1 on the second execution, thus hloc1 is
really being freed first, no GPF occurs until the LocalFree of hloc2.

-----------------------------------------------------------------------

So my question is: eventhough these are "Local" memory objects, why are the
hloc handles the same...except one, and is there a
flag or setting I can use which will not cause a GPF on
the reuse of handles? According to the MS references,
a memory handle can only be LocalFree()'d once...but
why then does LocalAlloc return the same handles?? Are
the handles considered different since the function is
called again later?

Thanks for any help...

Doug Stark

0 new messages