Keep getting error when using delete op

Started by
9 comments, last by tjrr3d 18 years, 1 month ago
I have a class called SquirrelGame. I create an instance by saying: SquirrelGame *sq = new SquirrelGame(); It does its thing and when its done I call: delete sq; Everytime it throws an error in dbheap.c. Here's where it happens:

/*---------------------------------------------------------------------------
 *
 * Memory management
 *
 --------------------------------------------------------------------------*/

/***
*static int CheckBytes() - verify byte range set to proper value
*
*Purpose:
*       verify byte range set to proper value
*
*Entry:
*       unsigned char *pb       - pointer to start of byte range
*       unsigned char bCheck    - value byte range should be set to
*       size_t nSize            - size of byte range to be checked
*
*Return:
*       TRUE - if all bytes in range equal bcheck
*       FALSE otherwise
*
*******************************************************************************/
static int __cdecl CheckBytes(
        unsigned char * pb,
        unsigned char bCheck,
        size_t nSize
        )
{
        int bOkay = TRUE;
        while (nSize--)
        {
            if (*pb++ != bCheck)   <-------HERE!!!!!
            {
/* Internal error report is just noise; calling functions all report results - JWM */
/*                _RPT3(_CRT_WARN, "memory check error at 0x%p = 0x%02X, should be 0x%02X.\n", */
/*                    (BYTE *)(pb-1),*(pb-1), bCheck); */
                bOkay = FALSE;
            }
        }
        return bOkay;
}

On the line if(*pb++ != bCheck) it says pb=0xccccccc8 <Bad Ptr> If I comment the delete sq line out, it works fine, but of course its leaking memory. Why would it throw this error? I've tried everything!
Advertisement
0xCCCCCCC8 is 0xCCCCCCCC - 5. 0xCCCCCCCC indicates an uninitialized variable.
The -5 offset would come from the internals of the memory allocation routine.

Diagnostic: This probably isn't the actual code you're using, is it? It would seem that sq is uninitialized at the time of the delete.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
hmmmm. Very strange. An error box pops up and says:
Unhandled exception at 0x1020a702 (msvcr71d.dll) in Tylers_Toy2.exe: 0xC0000005: Access violation reading location 0xcdcdcdc8.

I tried to make it just a straight object, insted of a pointer and now the value has changed to 0xcdcdcdc8.

SquirrelGame *sq = new SquirrelGame( CORE );			if (sq) 			{																	sq->Start();												MasterTime = 0;				prevNoMotion_Time = SDL_GetTicks();			}			else 			{				SDL_Color c = {255,255,255};				CORE->PrintString("Arial.ttf", 14, 300, 240, "ERROR!", c);				SDL_Delay(2000);			}			delete sq;


I know the variable is initialized because the program runs. Debugging has verified that it is initialized. Unless I somehow need to clear the space before I use it.

Also, it works if I just comment out delete.
Set on data breakpoint on sq so that it will stop whenever sq is changed so that you can find where it is being messed up.
.
if I just flat out say:
SquirrelGame *sq = new SquirrelGame(CORE);
if(sq)
delete sq;


I get the same error. Man this is annoying!
The problem may be inside your SquirrelGame destructor.
Doubt it

SquirrelGame::~SquirrelGame()
{
}
Quote:Original post by tjrr3d
Doubt it

SquirrelGame::~SquirrelGame()
{
}


Does the class contain any members that are implicitly destoyed?
I'm not sure what you mean exactly aren't all members declared on the stack implicilty destroyed when the class is destroyed?

I have several members that allocate memory, which I destroy inside my SquirrelGame.
Quote:Original post by tjrr3d
I'm not sure what you mean exactly aren't all members declared on the stack implicilty destroyed when the class is destroyed?

I have several members that allocate memory, which I destroy inside my SquirrelGame.


Just bcause your destructor is empty doesn't mean it does nothing, so the problem could be in the destructors of the members.

And the members are whereever the object is, if the object is on the heap so will its members, and the same with stack objects.

This topic is closed to new replies.

Advertisement