Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Possible causes for the 'delete ptr;' to core?? (http://www.velocityreviews.com/forums/t459770-possible-causes-for-the-delete-ptr-to-core.html)

qazmlp1209@rediffmail.com 01-11-2007 05:35 AM

Possible causes for the 'delete ptr;' to core??
 
My program cores at the 'delete ptr;' statement.
I could think of the following possibilities for the core/undefined
behaviour:
- delete twice
- new/malloc is done, but subsequently free/delete is used.
- 'new' is done; Pointer reference is changed; 'delete' is done;

What else can be the reason?


Ivan Novick 01-11-2007 05:37 AM

Re: Possible causes for the 'delete ptr;' to core??
 

qazmlp1...@rediffmail.com wrote:
> My program cores at the 'delete ptr;' statement.
> I could think of the following possibilities for the core/undefined
> behaviour:
> - delete twice
> - new/malloc is done, but subsequently free/delete is used.
> - 'new' is done; Pointer reference is changed; 'delete' is done;
>
> What else can be the reason?

You can find the reason your self by running your program with a memory
checker... for example this one http://www.valgrind.org

----
Ivan
http://www.0x4849.net


Jacek Dziedzic 01-11-2007 06:48 AM

Re: Possible causes for the 'delete ptr;' to core??
 
qazmlp1209@rediffmail.com wrote:
> My program cores at the 'delete ptr;' statement.
> I could think of the following possibilities for the core/undefined
> behaviour:
> - delete twice
> - new/malloc is done, but subsequently free/delete is used.
> - 'new' is done; Pointer reference is changed; 'delete' is done;
>
> What else can be the reason?


Using delete on a pointer not obtained from new, eg.
- from malloc,
- from new[],
- from somewhere else (that boils down to your case 3).

Or, if ptr points to an instance of the class, perhaps
the destructor is to blame.

As Ivan pointed out, if you are on a x86, you may try
valgrind to pinpoint the problem easily.

HTH,
- J.

Sylvester Hesp 01-11-2007 12:57 PM

Re: Possible causes for the 'delete ptr;' to core??
 

<qazmlp1209@rediffmail.com> wrote in message
news:1168493723.317457.36230@k58g2000hse.googlegro ups.com...
> My program cores at the 'delete ptr;' statement.
> I could think of the following possibilities for the core/undefined
> behaviour:
> - delete twice
> - new/malloc is done, but subsequently free/delete is used.
> - 'new' is done; Pointer reference is changed; 'delete' is done;
>
> What else can be the reason?
>


Another reason might be writing before or beyond the memory buffer ptr
points to (thus overwriting heap management data)

- Sylvester



Ron Natalie 01-11-2007 03:00 PM

Re: Possible causes for the 'delete ptr;' to core??
 
qazmlp1209@rediffmail.com wrote:
> My program cores at the 'delete ptr;' statement.
> I could think of the following possibilities for the core/undefined
> behaviour:
> - delete twice
> - new/malloc is done, but subsequently free/delete is used.
> - 'new' is done; Pointer reference is changed; 'delete' is done;
>
> What else can be the reason?
>


Another popular misbehavior is writing off the end of the
allocation, which corrupts the memory allocator:
i.e.,
char* x = new char[10];
strcpy(x, "abcdefghijklmnopqrstuvwxyz", 26);

Sylvester Hesp 01-11-2007 04:21 PM

Re: Possible causes for the 'delete ptr;' to core??
 
"Ron Natalie" <ron@spamcop.net> wrote in message
news:45A65128.5070502@spamcop.net...
> qazmlp1209@rediffmail.com wrote:
>> My program cores at the 'delete ptr;' statement.
>> I could think of the following possibilities for the core/undefined
>> behaviour:
>> - delete twice
>> - new/malloc is done, but subsequently free/delete is used.
>> - 'new' is done; Pointer reference is changed; 'delete' is done;
>>
>> What else can be the reason?
>>

>
> Another popular misbehavior is writing off the end of the
> allocation, which corrupts the memory allocator:
> i.e.,
> char* x = new char[10];
> strcpy(x, "abcdefghijklmnopqrstuvwxyz", 26);


Yes, or before the start, like I already said
In your example: x[-2] = 'a';
Granted, this happens less often, but is not less important :)

- Sylvester



Noah Roberts 01-11-2007 05:19 PM

Re: Possible causes for the 'delete ptr;' to core??
 

Sylvester Hesp wrote:
> "Ron Natalie" <ron@spamcop.net> wrote in message
> news:45A65128.5070502@spamcop.net...
> > qazmlp1209@rediffmail.com wrote:
> >> My program cores at the 'delete ptr;' statement.
> >> I could think of the following possibilities for the core/undefined
> >> behaviour:
> >> - delete twice
> >> - new/malloc is done, but subsequently free/delete is used.
> >> - 'new' is done; Pointer reference is changed; 'delete' is done;
> >>
> >> What else can be the reason?
> >>

> >
> > Another popular misbehavior is writing off the end of the
> > allocation, which corrupts the memory allocator:
> > i.e.,
> > char* x = new char[10];
> > strcpy(x, "abcdefghijklmnopqrstuvwxyz", 26);

>
> Yes, or before the start, like I already said
> In your example: x[-2] = 'a';
> Granted, this happens less often, but is not less important :)


It is the more difficult to find for sure and can explode deep inside
libary objects like vector or string. There is often no relationship
at all between the object that got overwritten and the one that
explodes. This type of bug can take hours or days to track down even
with the right tools. VS has settings you can make that check for
overruns and there are debugging applications that will do the same.



All times are GMT. The time now is 02:29 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.