Memory allocation problem

T

TBass

Ok, so I'm trying to understand why my debugger can't find the memory
address for the members of a structure.

typedef struct ADCLIST_el
{
struct ADCLIST_el *pPrev;
struct ADCLIST_el *pNext;

void *pData;

} ADCLIST_el;

int
adcList_Push( ADCLIST **ppHead, void *pData )
{
int ret;
ADCLIST *pNewNode;
ADCLIST *pTemp;

pNewNode = malloc( sizeof *pNewNode );


if ( pNewNode != NULL )
{

if ( *ppHead != NULL )
{
pTemp = *ppHead;
>>>> pTemp->pPrev = pNewNode;
}

...

(838.954): Access violation - code c0000005 (!!! second chance !!!)

The debugger returns this when I check the memory addresses of pTemp:

0:000> dt pTemp
Local var @ 0x12f990 Type ADCLIST_el*
0xbaadf00d
+0x000 pPrev : ????
+0x004 pNext : ????
+0x008 pData : ????
Memory read error baadf015

If pTemp is pointing to a valid ADCLIST structure, and pTemp has that
address, why don't its members have a memory address?

Outside of the debugger, the program runs fine without crashing, but
I'm assuming the debugger is telling me something I don't know. What
is it I'm not doing right for allocating the memory?

Thanks in advance,
TBJ
 
B

Ben Pfaff

TBass said:
The debugger returns this when I check the memory addresses of pTemp:

0:000> dt pTemp
Local var @ 0x12f990 Type ADCLIST_el*
0xbaadf00d
+0x000 pPrev : ????
+0x004 pNext : ????
+0x008 pData : ????
Memory read error baadf015

You really think that "bad food" is a valid memory address? I
think that your runtime system may be trying to tell you
something. (What that is I don't know: I don't recognize the
debugger or the environment in question.)
 
E

Eric Sosman

TBass said:
Ok, so I'm trying to understand why my debugger can't find the memory
address for the members of a structure.

typedef struct ADCLIST_el
{
struct ADCLIST_el *pPrev;
struct ADCLIST_el *pNext;

void *pData;

} ADCLIST_el;

int
adcList_Push( ADCLIST **ppHead, void *pData )
> [...]

Where's the definition of ADCLIST?

(In other words, please show actual code and not
a paraphrase. In this case, it would probably be worth
while to show the call to adcList_Push, and the code
that sets up its arguments.)
 
R

Richard Tobin

TBass said:
If pTemp is pointing to a valid ADCLIST structure,

Apparently it doesn't.
0:000> dt pTemp
Local var @ 0x12f990 Type ADCLIST_el*
0xbaadf00d

A Google search for 0xbaadf00d ("bad food") suggessts that it's the
value in uninitialised memory on Microsoft Windows.
Outside of the debugger, the program runs fine without crashing, but
I'm assuming the debugger is telling me something I don't know. What
is it I'm not doing right for allocating the memory?

Perhaps there is no bug, and the debugger is unable to handle some
compiler optimisation, or perhaps it doesn't match the version of the
compiler you used.

-- Richard
 
T

TBass

[snip]
(In other words, please show actual code and not
a paraphrase. In this case, it would probably be worth
while to show the call to adcList_Push, and the code
that sets up its arguments.)
[/snip]

This is the ADCLIST structure:

typedef struct ADCLIST_el
{
struct ADCLIST_el *pPrev;
struct ADCLIST_el *pNext;

void *pData;

} ADCLIST_el;

typedef ADCLIST_el ADCLIST;



This is the complete Push function:

int
adcList_Push( ADCLIST **ppHead, void *pData )
{
int ret;
ADCLIST *pNewNode;
ADCLIST *pTemp;

pNewNode = malloc( sizeof *pNewNode );


if ( pNewNode != NULL )
{

if ( *ppHead != NULL )
{
pTemp = *ppHead;
pTemp->pPrev = pNewNode;
}

pNewNode->pData = pData;
pNewNode->pNext = *ppHead;
pNewNode->pPrev = NULL;
*ppHead = (ADCLIST *)pNewNode;
ret = TRUE;
}
else
{
ret = FALSE;
}

return ret;

} /* adcList_Push */



This is the function that calls adcList_Push:

unsigned int
dControls_AddControl( DNODE **ppNode,
DCONTROL *s )
{
int ret;
int lret;
int pret;
DNODE *dnode;
ADCLIST *listControls;
DCONTROL *dcontrol;

#if DAEDALUS_DEBUG
printf( "\ndControls_AddControl...BEGIN...bFunctionsLoaded=%d",
bFunctionsLoaded );
#endif


if ( s->type > DCONTROLS_MAXTYPE )
{
ret = DCONTROLS_ERR_INVALIDTYPE;
goto Quit;
}

/* ARE OUR FUNCTIONS LOADED? */
if ( !bFunctionsLoaded )
{
lret = dControls_loadFunctions();
if ( lret == FALSE )
{

#if DAEDALUS_DEBUG
printf( "\ndControls_AddControl...FAILED TO LOAD CONTROLS\n" );
#endif

ret = DCONTROLS_ERR_FAILLOADFNC;
goto Quit;
}
}


dnode = (DNODE *)*ppNode;

if ( dnode != NULL )
{

/* CREATE A NEW CONTROL ON THE HEAP */
dcontrol = malloc( sizeof(DCONTROL) );
if ( dcontrol == NULL )
{
ret = FALSE;
goto Quit;
}


/* COPY THE PROPERTIES OF THE SPECIFIED CONTROL */
/* TO THE NEW LOCATION */
dControls_copyControl( s, dcontrol );

/* IS THIS A CUSTOM CONTROL, OR DO WE NEED TO */
/* GET THE POINTER FOR ITS FUNCTIONS? */
if ( dcontrol->type < 255 )
{
/* DRAW FUNCTION */
dcontrol->pDraw = funcDraw[dcontrol->type];
dcontrol->pSignal = funcSignal[dcontrol->type];
dcontrol->pHandleKeyboardEvents = funcKeyboard[dcontrol->type];

/* THESE VALUES MUST BE INITIALLY FALSE */
dcontrol->bInInput = FALSE;
dcontrol->pInputBuffer = NULL;

/* DOES THIS FUNCTION HAVE A PRE-PROCESS FUNCTION? */
if (funcProcess[dcontrol->type] != NULL )
{

#if DAEDALUS_DEBUG
printf( "\ndControls_AddControl...Calling Pre-Process function: %d
\n", dcontrol->type );
#endif

pret = funcProcess[dcontrol->type]( ppNode,
&dcontrol,
0,
0 );
if ( pret <= 0 )
{
free( dcontrol );
ret = pret;
goto Quit;
}

}

}


/* PUSH THE CONTROL ONTO THE LIST */
listControls = dnode->listControls;

adcList_Push( &dnode->listControls, dcontrol );


ret = TRUE;

} /* if ret */
else
{
#if DAEDALUS_DEBUG
printf( "\ndControls_AddControl...DNODE WAS NULL\n" );
#endif
ret = FALSE;
}
Quit:

#if DAEDALUS_DEBUG
printf( "\ndControls_AddControl...END return: %d\n", ret );
#endif


return ret;

} /* dControls_AddControl */
 
T

TBass

[snip]
You really think that "bad food" is a valid memory address?
[/snip]

[snip]
A Google search for 0xbaadf00d ("bad food") suggessts that it's the
value in uninitialised memory on Microsoft Windows.
[/snip]

Thanks! I did not know the meaning of "bad food" and didn't look at
the address closely enough to even catch it was trying to tell me
something. Knowing what the address meant, I was able to find the
problem.

Thanks everyone,
TBJ
 
E

Eric Sosman

TBass said:
[...]

This is the function that calls adcList_Push:

unsigned int
dControls_AddControl( DNODE **ppNode,
DCONTROL *s )
{
int ret;
int lret;
int pret;
DNODE *dnode;
[...]
dnode = (DNODE *)*ppNode;

Aside: What's the cast for?
if ( dnode != NULL )
{
[...]
/* PUSH THE CONTROL ONTO THE LIST */
listControls = dnode->listControls;

adcList_Push( &dnode->listControls, dcontrol );

Okay, we're making progress. One step at a time, but
we're making it. The next question is: What reason do we
have to believe that dnode->listControls has been set up
with a valid value? That is, where did ppNode come from,
and where was the DNODE* that it points to initialized, and
where was the DNODE that the pointer in turn points to
initialized?

You have found a tube of poisonous toothpaste on the
store shelf. Where did the tube come from? It was part
of the truckload that came in yesterday evening. Where did
the truck come from? It was a shipment from the warehouse
in East Overshoe. Where did E.O. get the goods? They were
delivered by Canny Cal's Country Cargo Connection. Where did
CCCCC pick them up? And so on, until you've traced the
toothpaste to its source. Backtrack the bogus data in the
same way: adcList_Push got it from dcontrols_AddControl,
which got it from ...
 
T

TBass

[snip]
we're making it. The next question is: What reason do we
have to believe that dnode->listControls has been set up
with a valid value? That is, where did ppNode come from,
[/snip]

That was the problem. For the first item on the list, the "head" value
passed to _Push needs to NULL. I initialized the value to NULL, and
the program ran in the debugger without problem.

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top