when delete do not call destructor

S

S S

Hi

What can be the situations when delete does not call destructor? I
have overloaded operator delete.

class A{
public:
....new is also overloaded
....
void operator delete(void* p, size_t n) {deallocate(p, n);}
...
...
~A() {}
};

When I call
delete A;

It gives error,
*** glibc detected *** free(): invalid pointer: 0x00000003ffff01480

It gives below stack trace,
#0 0x000000328d42e25d in raise () from /lib64/tls/libc.so.6
#1 0x000000328d42fa5e in abort () from /lib64/tls/libc.so.6
#2 0x000000328d4635e1 in __libc_message () from /lib64/tls/libc.so.6
#3 0x000000328d4691ee in _int_free () from /lib64/tls/libc.so.6
#4 0x000000328d469586 in free () from /lib64/tls/libc.so.6
#5 0x0000002a9bf9ff3e in operator delete () from /usr/lib64/libstdc+
+.so.5

I think delete do not take me to my deallocation routine and try to
free the memory itself which of course do not exist. Memory exist is
my own created heap which must be deallocated (effectively my
deallocation routine should be called)

Besides setting breakpoint in destructor does not reach there. I think
delete must always call destructor?

Thanks
 
S

S S

delete operator and operator delete are different things.  The former
calls the destructor, the latter is your overloaded deallocation function..





That's almost impossible.  'A' is the name of the type.  Please post
real code.




How was the pointer obtained?  Did you 'new' it?  Or did you just take
the address of a static variable or an automatic variable?  Are you by
chance trying to delete something twice?








Yes.  But I don't think it's the issue here.  It calls the destructor
and *then* deallocates memory.  You will not see the destructor in the
call stack *unless* you put the breakpoint in the destructor itself.

V

Hi Victor

I can not write full code as it is 20000 lines kind of copywrite
thing. But I am writing below required details,
class A{
public:
void *operator new(size_t, void *p) {return(p);}

void *operator new(size_t n) throw(std::bad_alloc)
{ My_allocation_routine(&someInternalVar, n);}

void operator delete(void* p, size_t n) {My_deallocation_routine(p,
n);}
...
...
~A() {}
};

I did,

A* obj = new A;
delete obj;

I set breakpoint in destructor of this class, it does not reach there.
Don't know why?
The other error I get is the one I wrote above in my last mail, and I
assume I get that *** free ** error because My_deallocation_routine is
not called. And My_allocation_routine do not use malloc to create
heap. It is direct mmap call with fixed addresses. When free() is
invoked for the pointer which is created in my_own_heap, it won't find
that pointer is sbrk() heap and hence shows such error.

Thanks
 
S

SG

I can not write full code as it is 20000 lines kind of copywrite
thing.

Nobody would like to see those 20000 lines anyways. Victor said
"Please post
real code.". He wanted to see a small program that is compilable or
at least supposed to compile.
class A{
public:
void *operator new(size_t, void *p) {return(p);}

void *operator new(size_t n) throw(std::bad_alloc)
{ My_allocation_routine(&someInternalVar, n);}

void operator delete(void* p, size_t n) {My_deallocation_routine(p,
n);}
..
..
~A() {}

};

This is not "real code". It lacks #include directives, contains ".."
which doesn't parse, and misses a definition of My_allocation_routine.


Cheers!
SG
 
S

S S

What, your C++ compiler doesn't support the new C++0x ".." directive,
which has it read the mind of the poster and fill in the blanks?

Seriously, to post "real code" would require spending some "real time"
(about 3 minutes) putting together a stand-alone code sample that
demonstrates the problem. The time spent putting that together might even
lead to a solution without having to post anything here.

I will offer one bit of help: debuggers often have limitations about what
can be stopped at. Best way to find out if that's the cause of the missed
breakpoint is to write a separate code example to see if that's the case.
As usual, remove everything that doesn't make the problem go away. My
guess is that your debugger can't breakpoint on inline functions, at least
not without some special compiler option.

The same code is working for all other objects, and with small piece
of code I am not able to replicate it. It's kind of weird issue and I
don't know why destructor is not called when delete is asked.
 
S

S S

S said:
[..]
The same code is working for all other objects, and with small piece
of code I amnotable to replicate it. It's kind of weird issue and I
don't know whydestructorisnotcalled whendeleteis asked.

Just thought of a possible problem: if you derive your class A from
another class that you then polymorphicallydelete(through a pointer to
the base class), then if the d-tor isnotvirtual, the behaviour is
undefined.  Just another thing to check.  Notsure how the overridden
operatordeletewould work in that case, though.

V

It is not derived class and neither it is base class. I should have
mentioned that before. I don't understand why delete is just being
treated as free() and hence crashing, neither any multiple deletions
kind of thing, I do not see any unusual thing so far. Thanks in
advance.
 
B

Bo Persson

S said:
The same code is working for all other objects, and with small piece
of code I am not able to replicate it. It's kind of weird issue and
I don't know why destructor is not called when delete is asked.

If you can't replicate the problem in a smaller piece of code, the
problem is very often somewhere else than in that small piece of code.
For example some memory overwrite that damages the heap long before
you reach the delete statement.


Bo Persson
 
S

S S

If you can't replicate the problem in a smaller piece of code, the
problem is very often somewhere else than in that small piece of code.
For example some memory overwrite that damages the heap long before
you reach thedeletestatement.

Bo Persson

If I remove this delete statement and allow some memory leak, there is
no issue and application runs very fine. Purify doesn't say anything.
It's mysterious!
 
A

Alf P. Steinbach

* S S:
Hi

What can be the situations when delete does not call destructor? I
have overloaded operator delete.

class A{
public:
...new is also overloaded
...
void operator delete(void* p, size_t n) {deallocate(p, n);}
..
..
~A() {}
};

When I call
delete A;

It gives error,
*** glibc detected *** free(): invalid pointer: 0x00000003ffff01480

It gives below stack trace,
#0 0x000000328d42e25d in raise () from /lib64/tls/libc.so.6
#1 0x000000328d42fa5e in abort () from /lib64/tls/libc.so.6
#2 0x000000328d4635e1 in __libc_message () from /lib64/tls/libc.so.6
#3 0x000000328d4691ee in _int_free () from /lib64/tls/libc.so.6
#4 0x000000328d469586 in free () from /lib64/tls/libc.so.6
#5 0x0000002a9bf9ff3e in operator delete () from /usr/lib64/libstdc+
+.so.5

I think delete do not take me to my deallocation routine and try to
free the memory itself which of course do not exist. Memory exist is
my own created heap which must be deallocated (effectively my
deallocation routine should be called)

Besides setting breakpoint in destructor does not reach there. I think
delete must always call destructor?

Yes.

It seems you have Undefined Behavior and/or some tool usage problem (debugger).

But generally UB is indicated, e.g. that your call stack is seriously messed up,
which also is indicated by inability to reproduce in small example.


Cheers & hth.,

- Alf
 
S

S S

* S S:




What can be the situations when delete does not call destructor? I
have overloaded operator delete.
class A{
public:
...new is also overloaded
...
void operator delete(void* p, size_t n) {deallocate(p, n);}
..
..
~A() {}
};
When I call
delete A;
It gives error,
*** glibc detected *** free(): invalid pointer: 0x00000003ffff01480
It gives below stack trace,
#0  0x000000328d42e25d in raise () from /lib64/tls/libc.so.6
#1  0x000000328d42fa5e in abort () from /lib64/tls/libc.so.6
#2  0x000000328d4635e1 in __libc_message () from /lib64/tls/libc.so.6
#3  0x000000328d4691ee in _int_free () from /lib64/tls/libc.so.6
#4  0x000000328d469586 in free () from /lib64/tls/libc.so.6
#5  0x0000002a9bf9ff3e in operator delete () from /usr/lib64/libstdc+
+.so.5
I think delete do not take me to my deallocation routine and try to
free the memory itself which of course do not exist. Memory exist is
my own created heap which must be deallocated (effectively my
deallocation routine should be called)
Besides setting breakpoint in destructor does not reach there. I think
delete must always call destructor?

Yes.

It seems you have Undefined Behavior and/or some tool usage problem (debugger).

But generally UB is indicated, e.g. that your call stack is seriously messed up,
which also is indicated by inability to reproduce in small example.

Cheers & hth.,

- Alf

--
Due to hosting requirements I need visits to [http://alfps.izfree.com/].
No ads, and there is some C++ stuff! :) Just going there is good. Linking
to it is even better! Thanks in advance!- Hide quoted text -

- Show quoted text -

Thanks everyone. I found the problem. Definition of class was missing
from the file where I was trying to delete the pointer. c++ shows
error in case of "new" if definition of constructor is not found, but
just gives waring in case of delete !!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top