Here you go...
***Here are portions of the two suspect pieces of code. The first code repetitively allocates
storage from the heap, the second code setment creates the private heap and obtains one
(the initial) chunk from it. The first code section typically fails (returns NULL) on the
second or third call to this routine.
***Note this is not my original code but something purloined from a database project
found on the web. I've modified much of the code but these sections are pretty vanilla.
===========================
***This is the routine, called multiple times, which will soon fail fail under debug, with
HeapAlloc returning a NULL pointer. Works great when run 'normally' even though the
executable file was compiled with debug settings.
***Note you can see just below where I'm just beginning to play around with the
"IsDebuggerPresent" call mentioned to my by "frankie".
/** first code segment **/
BOOL db_RecordAdd(NODE node) /* called from various routines; builds and chains NODEs in memory */
{
LPNODE tmp;
//#define _WIN32_WINNT 0x0501
BOOL bStat;
// bStat = IsDebuggerPresent();
// if (bStat == FALSE)
// {
// WriteListBox("1", "database.c: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.");
// ghDataHeap = GetProcessHeap();
// }
// ghDataHeap = GetProcessHeap(); /* PhilG added - get the address of the heap... */
/* will run correctly normally but fails under debug??? */
tmp->NextNode = NULL;
/* alloc from that gotten earlier in MemoryBaseCreate */
/* HeapAlloc(heap, flags, bytes */
tmp->NextNode = HeapAlloc(ghDataHeap, 0, sizeof(NODE));
if(tmp->NextNode == NULL) /* did we get heap storage allocated */
{
WriteListBox("1", "database.c: db_RecordAdd() HeapAlloc failed.");
memset(&szMessage, ' ', sizeof(szMessage));
(void) wsprintf(szMessage, "database.c: db_recordAdd(): HeapAlloc failed.");
(void) MessageBox(NULL, szMessage, "Error...", MB_OK);
_dnnest;
return FALSE; /* HeapAlloc failed for some reason */
}
...
...
...
=========================================
=========================================
***This is where the private heap space is initially allocated. One initlai chunk is gotten
here, the rest and majority of calls to HeapAlloc are in the code just above.
/** second code segment **/
BOOL MemoryBaseCreate(void)
{
/*
allocate space for "NUM_NODES" NODEs (managed files);
this will yield about a 53K program with no files yet appended...
*/
/* HeapCreate(optons, initial size, maxmum size where 0 = heap can grow in size */
ghDataHeap = NULL; /* address of heap memory */
ghDataHeap = HeapCreate(0, (sizeof(NODE) * (NUM_NODES+2)), 0); /* create a private heap which can grow */
if (ghDataHeap == NULL)
{
WriteListBox("1", "MemoryBaseCreate() HeapCreate failed.");
gbHaveHeapMemory = FALSE;
_dnnest;
return FALSE;
}
gbHaveHeapMemory = TRUE; /* have block of heap memory */
/*
use some of the heap space; returns pointer to allocated block
*/
// looks like GetProcessHeap not needed for private heaps
// ghDataHeap = GetProcessHeap(); /* have to get the address of the heap... */
/* HeapAlloc(heap, flags, size */
gpstNode = 0;
gpstNode = HeapAlloc(ghDataHeap, 0, sizeof(gpstNode));
if(gpstNode == NULL)
{
WriteListBox("1", "MemoryBaseCreate() HeapAlloc failed.");
_dnnest;
return FALSE;
}
/*
This will be the first NODE On the chain. Additional NODEs will be allocated
in db_AddRecord. Save the address of this first NODE block so we can
access it and the entire chain later.
*/
if (!gbSaveFirstNodeAddr) /* just through here one time per database open */
{
giNodeChain = (int) gpstNode; /* save memory address just obtained */
gbSaveFirstNodeAddr = TRUE;
}
/*
Initialize the just gotten NODE structure
*/
...
...
...
Thanks again.