Sign in to follow this  
tjrr3d

Keep getting error when using delete op

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this