• Advertisement

Archived

This topic is now archived and is closed to further replies.

Deleting a pointer results in 0xdddddddd?

This topic is 5004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, so I''m running a program with a few pointers being newed and deleted as the windows timer runs. Anyways, every now and then one of the deleted pointers ends up pointing to 0xdddddddd as opposed to 0x00000000 (NULL, of course). This is creating problems because my program won''t spit out the correct result when I have an if statement: if(pointer != NULL). Does anyone know what the problem might be? Oh, and I am using Visual C++.

Share this post


Link to post
Share on other sites
Advertisement
Sometimes, in debug mode, pointer values are set to predetermined values by the compiler depending on their status (in this case, a deleted pointer is set to 0xDDDDDDDD). Your best bet it to set it to 0 after you delete.

Share this post


Link to post
Share on other sites
Need more info. Pointers aren''t set to null implicitly when you delete data. Usually you have to set the pointer to NULL after you delete what it''s pointing to. Are you doing this?

Share this post


Link to post
Share on other sites
quote:
Original post by Cypher19
Anyways, every now and then one of the deleted pointers ends up pointing to 0xdddddddd as opposed to 0x00000000 (NULL, of course)

As mentioned, ''delete'' does not set the pointer to NULL, you have to remember to do that yourself. In debug mode various compilers/debuggers may insert special memory codes to help you catch these type of problems. So VS sets the deleted pointer to 0xdddddddd, but you''re still supposed to set it to NULL yourself. In release mode the pointer will be left unchanged, and pointing to a now-invalid area of memory.

Other debug memory codes include ones like 0xcdcdcdcd and 0xcccccccc, their meaning is listed on MSDN for VS.

Share this post


Link to post
Share on other sites
Thanks for the suggestions, but even when setting the pointer to null right after deletion, it still causes an error.

Fwiw, here''s the code:


void c_LinkedList<t_var>::DestroyList(s_LinkedListNode<t_var> *List, bool DeleteInfo)
{
if(List != NULL)
{
DestroyList(List->Next, DeleteInfo);
if(DeleteInfo)
{
if(List->Variable != NULL)
{
delete List->Variable;
List->Variable = NULL;
}
}
delete List;
List = NULL;
NumOfNodes--;
}
}


The intent of it is to delete an entire linked list, and if deleteinfo is set to true, delete the contents of the list as well.

Oh, and the unhandled exception occurs when the recursion is called.

Share this post


Link to post
Share on other sites
According to the recursion, the list will never be deleted, and when List == NULL, the deletion part is skipped (at least I think). May not be the cause of the unhandled exception, but...


-- Fyhuang, president, Altitude Technologies

Altitude Technologies: http://www.hytetech.com/altitude
Altitude Forums: http://s8.invisionfree.com/Altitude_Forums/

Share this post


Link to post
Share on other sites
OrangyTang: My favorite memory code was 0xdeadbeef on AIX boxes for uninitialized memory. The first time I did a memory dump in the debugger I thought I was loosing my mind...

Share this post


Link to post
Share on other sites
delete should not be changing the value of the pointer. the VC++ debugger sets the memory the pointer pointed to to 0xdddddddd so if you access it again, you will see you are accessing already deleted data.

Share this post


Link to post
Share on other sites
I suspect that you don't have a null-terminated list for whatever reason so I'd look at at your other list code. The only other thing is if the list was really big you could be running out of stack space becasue of your recursion but I suspect that not the problem.

Also delete NULL; is defined as being OK by the C++ standard so you can safely compress something like


if ( foo != NULL ) {
delete foo;
foo = NULL
}

to

delete foo;
foo = NULL

or even

delete foo, foo = NULL;




Edit: typos

[edited by - mauman on June 10, 2004 3:02:44 PM]

Share this post


Link to post
Share on other sites
The list actually is null terminated.

And it wouldn''t be because of stack limitations (yet) because right now I''m only deleting two objects inside the linked list.

Anyways, I found out what the problem was:

When destroylist is called inside the class, it sends in the beginning of the list (''Beginning'', a private variable of the class). The function runs through all happy and fine, but the problem came when I had to call the function again (with deleteinfo off.), and ''Beginning'' pointed to an unallocated spot in memory, and its address wasn''t NULL, so that''s why I got an unhandled exception. So I just added Beginning = NULL inside the class after where DestroyList is called.

Share this post


Link to post
Share on other sites
The reason why it wasn''t working before was because you''re passing the pointers by value, not by reference, so when you NULL out the pointer in the function, it doesn''t NULL out the original pointer. Here''s a clearer example:
int anInt = 5;
int aPointer = &anInt;

DoStuffWithAPointer(aPointer); // See below


...

void DoStuffWithAPointer(int *pointer)
{
*pointer = 6; // anInt now equals 6


pointer = NULL; // Local variable pointer now equals NULL

// but global variable aPointer does NOT equal NULL

}


To make your original function work as planned with a kludge (i.e. adding the line Beginning = NULL; after the call to DestroyList), pass the pointer by reference:

void c_LinkedList<t_var>::DestroyList(s_LinkedListNode<t_var> * & List, bool DeleteInfo) // Note the ''&''

Share this post


Link to post
Share on other sites

  • Advertisement