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?
"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.
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.
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.
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.
Are you allocating with new and deleting with delete[], or vice versa? You have to match new[n] with delete[] and new with delete.
Are you ever using arrays on the stack, or pointers to stack variables? Check you're not overrunning them.
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.
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.
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.