Segfaults on pointer deletion

Hey Everyone,

I have a check similar to this:

if (ptr)
{
   delete ptr;
   ptr = null;
}

When I'm debugging in AIX (using dbx), if I attempt to print the value of 'ptr' it says "ptr is not defined" - however, it still enters that if block. So, I'm getting segfaults on the delete statement. Can anyone suggest why the if() block is entered?

Simple reason for SEGV on delete can be ptr is not obtained with 'new' -- like automatic array.
Or you should have delete[] instead?

Otherwise, what is the type of 'ptr'? If it's pointer to object, look at destructor code.

Hey, thanks for the reply. I originally thought something similar to this, but the code works fine in Windows.

"Works under windows" means less than nothing. Win let me get away with deleting my video surface, 60 times per second, while it was in use, without crashing. The only symptoms were mysterious heap errors much, much later... I never found the true source of the faults until I ported it to UNIX, where it immediately crashed precisely in the place my code was doing ridiculous things.

Any idea what this would mean?

I've checked in Windows, and some of the member pointers of this object I'm deleting are NULL - would that cause an issue on UNIX?

Segfaults happen when you try to use memory areas you aren't assigned to. Nothing more, nothing less. That your program's crashing now, and not in Windows, may be because you didn't check the return value of a failed call and stored invalid values, or mangled a pointer because of integer size differences between Windows and AIX, or overran the end of an array and mangled your stack, or deleted something you never allocated and messed up your heap. On some platforms nothing happens; the function doesn't fail and the failing case never gets tested, or the integer sizes are just right to hold a pointer, you dump garbage on areas of the stack that don't matter, or the heap tolerates your misbehavior(for a time). But any change in circumstances, different compiler or different libraries or different architecture or different OS or different version, can bring out bugs that were waiting to happen. There's nothing magical or uniquely UNIX about it.

There's a few possibilities for what's causing your program to access invalid memory.

  1. Is the pointer you're deleting valid? Make sure you're deleting the same pointer you started with. The wrong pointer is just as bad as a NULL pointer.
  2. What does your destructor do? If it's deleting anything check that it's deleting what it started with too. delete doesn't care what's stored in it as long as the memory itself is valid, but the destructor might.
  3. Are you allocating with new and deleting with delete[], or vice versa? You have to match new[n] with delete[] and new with delete.
  4. Are you ever using arrays on the stack, or pointers to stack variables? Check you're not overrunning them.
  5. What are you deleting when? Some heap implementations don't do thorough checking, so deleting a bad pointer long ago can cause heap errors far later.

But I can't tell a single thing from your error message because you haven't posted all of your code. This could be a problem in far deeper or different code than you might think, and all these are just guesses.

[/list]

  1. Yes, it's the same pointer
  2. Destructor doesn't do anything
  3. Everything is allocated with 'new' and deleted with 'delete'
  4. Yes, but they appear to be within bounds. I'm not sure if there's a special way to delete an object that contains arrays?
  5. Not sure about this one.

Is it invalid to delete an object that contains pointers to NULL? The object I'm deleting has 3 members that are pointing to 0x0000000.

Please post your code. I can only guess what might be going wrong at this point.

You've checked the value the pointer started as? You're sure it didn't mysteriously change anywhere? How did you check?

Literally nothing?

I'm not sure what this has to do with my question, since objects you created with new aren't stack objects, but no. There's no special way. If you made it with new, you can free it with delete.

No. Delete doesn't check or use any of the members, that's the destructor's job.

delete doesn't care about the contents. What matters is what the destructor does.

I used up everything my crystal ball had to offer in the last post. I can't and won't answer any more of your questions until you post your code.

Hi ctote,

if a ptr is NOT NULL, then it is true, (for ex. containing a junk values, aka bad pointer). if you did not define your ptr to NULL when you declare/free them, you should be careful about re-using them. Try to print the 'ptr' before entering the 'if block' and after you exiting the 'if block' and see if there is any difference.

Cheers,
Gaurav.