Segfaults on pointer deletion


 
Thread Tools Search this Thread
Top Forums Programming Segfaults on pointer deletion
# 1  
Old 03-06-2011
Segfaults on pointer deletion

Hey Everyone,

I have a check similar to this:
Code:
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?
# 2  
Old 03-06-2011
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.
# 3  
Old 03-06-2011
Hey, thanks for the reply. I originally thought something similar to this, but the code works fine in Windows.
# 4  
Old 03-07-2011
"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.
# 5  
Old 03-09-2011
Any idea what this would mean?

Quote:
Illegal instruction in . at 0x11e5948c0 ($t1)
0x11e5948c0 (???) 00000001 Invalid opcode.

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?
# 6  
Old 03-09-2011
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.

Last edited by Corona688; 03-09-2011 at 03:57 PM..
# 7  
Old 03-09-2011
Quote:
Originally Posted by Corona688
  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.
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

advanced deletion

How can i delete the contents of the directory except one file? (8 Replies)
Discussion started by: proactiveaditya
8 Replies

2. Shell Programming and Scripting

Deletion of records

Hi, I have file with footer (5records) at end of the file. I want to delete the footer as well as the empty records between the main records and footer record.(The gap between main records and Footer records is dynamic) Example: MainRecord1 "115494",","FAELD","CT","... (12 Replies)
Discussion started by: vsairam
12 Replies

3. Shell Programming and Scripting

Deletion of a particular character

How to delete a particular character in a file and then removing the spaces created by that removal.I am working with a text file containing nucleotide sequences. I have tried sed command but it replaces N with spaces. example:I want to delete all the N without creating a space after its ... (1 Reply)
Discussion started by: ankitachaurasia
1 Replies

4. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

5. Forum Support Area for Unregistered Users & Account Problems

deletion ofsunburntYux

Could you please close SunburntYux as I would like to use the emaill address on this account to register FloridaBSD, (0 Replies)
Discussion started by: SunBurntYux
0 Replies

6. Shell Programming and Scripting

Help: deletion of record

I have created a address book file. Insertion of Record (using >> symbol)and searching of record (using grep command) into the address book is also working correctly. Now I want to delete a record into a file. Any body can help me in this case without using sed and awk18 18. Thanks and best... (24 Replies)
Discussion started by: murtaza
24 Replies

7. UNIX for Advanced & Expert Users

Deletion of Logs

Can someone suggest on this script : let's say the logs files are available for Jan 07, Dec 06, Nov 06, Oct 06 the script should identify the latest months logs, i.e Jan 07 it should then delete anything older than 2 months, which will be logs for Nov 06 & Oct 06. (40 Replies)
Discussion started by: srirams
40 Replies

8. Programming

memcpy segfaults, but not in windows

Hi Having a lil trouble with a rather simple application I'm writing. It so happens that I have to copy some data using memcpy() and so far I've been doing just fine compiling it with VC.Net and running it on Windows XP. Now I'm trying to port the thing to Solaris (which shouldn't really be too... (3 Replies)
Discussion started by: khoma
3 Replies

9. Shell Programming and Scripting

Regarding deletion of old files

Hi, I have a list of directories which contain old files that are to be deleted. I put the list of all directories in a txt file and it is being read by a script Iam searching for the files older than 60 days using mtime and then deleting it But all the files are getting deleted... (3 Replies)
Discussion started by: Chidvilas
3 Replies

10. Post Here to Contact Site Administrators and Moderators

Account Deletion...

Please delete my account? Thanks, so very much, really. (2 Replies)
Discussion started by: spaceshiporion
2 Replies
Login or Register to Ask a Question