Sponsored Content
Top Forums Programming Segfaults on pointer deletion Post 302503058 by Corona688 on Wednesday 9th of March 2011 02:49:54 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
MALLOC(3)						   BSD Library Functions Manual 						 MALLOC(3)

NAME
calloc, free, malloc, realloc, reallocf, valloc -- memory allocation SYNOPSIS
#include <stdlib.h> void * calloc(size_t count, size_t size); void free(void *ptr); void * malloc(size_t size); void * realloc(void *ptr, size_t size); void * reallocf(void *ptr, size_t size); void * valloc(size_t size); DESCRIPTION
The malloc(), calloc(), valloc(), realloc(), and reallocf() functions allocate memory. The allocated memory is aligned such that it can be used for any data type, including AltiVec- and SSE-related types. The free() function frees allocations that were created via the preceding allocation functions. The malloc() function allocates size bytes of memory and returns a pointer to the allocated memory. The calloc() function contiguously allocates enough space for count objects that are size bytes of memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes of value zero. The valloc() function allocates size bytes of memory and returns a pointer to the allocated memory. The allocated memory is aligned on a page boundary. The realloc() function tries to change the size of the allocation pointed to by ptr to size, and returns ptr. If there is not enough room to enlarge the memory allocation pointed to by ptr, realloc() creates a new allocation, copies as much of the old data pointed to by ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory. If ptr is NULL, realloc() is identical to a call to malloc() for size bytes. If size is zero and ptr is not NULL, a new, minimum sized object is allocated and the original object is freed. The reallocf() function is identical to the realloc() function, except that it will free the passed pointer when the requested memory cannot be allocated. This is a FreeBSD specific API designed to ease the problems with traditional coding styles for realloc causing memory leaks in libraries. The free() function deallocates the memory allocation pointed to by ptr. If ptr is a NULL pointer, no operation is performed. RETURN VALUES
If successful, calloc(), malloc(), realloc(), reallocf(), and valloc() functions return a pointer to allocated memory. If there is an error, they return a NULL pointer and set errno to ENOMEM. For realloc(), the input pointer is still valid if reallocation failed. For reallocf(), the input pointer will have been freed if realloca- tion failed. The free() function does not return a value. DEBUGGING ALLOCATION ERRORS
A number of facilities are provided to aid in debugging allocation errors in applications. These facilities are primarily controlled via environment variables. The recognized environment variables and their meanings are documented below. ENVIRONMENT
The following environment variables change the behavior of the allocation-related functions. MallocLogFile <f> Create/append messages to the given file path <f> instead of writing to the standard error. MallocGuardEdges If set, add a guard page before and after each large block. MallocDoNotProtectPrelude If set, do not add a guard page before large blocks, even if the MallocGuardEdges environment variable is set. MallocDoNotProtectPostlude If set, do not add a guard page after large blocks, even if the MallocGuardEdges environment variable is set. MallocStackLogging If set, record all stacks, so that tools like leaks can be used. MallocStackLoggingNoCompact If set, record all stacks in a manner that is compatible with the malloc_history program. MallocStackLoggingDirectory If set, records stack logs to the directory specified instead of saving them to the default location (/tmp). MallocScribble If set, fill memory that has been allocated with 0xaa bytes. This increases the likelihood that a program mak- ing assumptions about the contents of freshly allocated memory will fail. Also if set, fill memory that has been deallocated with 0x55 bytes. This increases the likelihood that a program will fail due to accessing mem- ory that is no longer allocated. MallocCheckHeapStart <s> If set, specifies the number of allocations <s> to wait before begining periodic heap checks every <n> as speci- fied by MallocCheckHeapEach. If MallocCheckHeapStart is set but MallocCheckHeapEach is not specified, the default check repetition is 1000. MallocCheckHeapEach <n> If set, run a consistency check on the heap every <n> operations. MallocCheckHeapEach is only meaningful if MallocCheckHeapStart is also set. MallocCheckHeapSleep <t> Sets the number of seconds to sleep (waiting for a debugger to attach) when MallocCheckHeapStart is set and a heap corruption is detected. The default is 100 seconds. Setting this to zero means not to sleep at all. Set- ting this to a negative number means to sleep (for the positive number of seconds) only the very first time a heap corruption is detected. MallocCheckHeapAbort <b> When MallocCheckHeapStart is set and this is set to a non-zero value, causes abort(3) to be called if a heap corruption is detected, instead of any sleeping. MallocErrorAbort If set, causes abort(3) to be called if an error was encountered in malloc(3) or free(3) , such as a calling free(3) on a pointer previously freed. MallocCorruptionAbort Similar to MallocErrorAbort but will not abort in out of memory conditions, making it more useful to catch only those errors which will cause memory corruption. MallocCorruptionAbort is always set on 64-bit processes. MallocHelp If set, print a list of environment variables that are paid heed to by the allocation-related functions, along with short descriptions. The list should correspond to this documentation. DIAGNOSTIC MESSAGES
SEE ALSO
leaks(1), malloc_history(1), abort(3), malloc_size(3), malloc_zone_malloc(3), posix_memalign(3), libgmalloc(3) BSD
Aug 13, 2008 BSD
All times are GMT -4. The time now is 05:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy