473,725 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloaded delete does not calls the destructor??

I have an overload delete operator as below

//////////////////////////////////
void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetM emoryHead(head_ type);
mmFree(&local_H ead,(char *)mem);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_ty pe);
}
/////////////////////
void* operator new(size_t sz, int head_Type) {
char *mem;
mmHead local_Head = CPRMemory::GetM emoryHead(head_ Type);
mem = mmAlloc(&local_ Head,sz);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_Ty pe);
if(!mem) cout<<"Out of Memory"<<endl;
return mem;
}
////////////////
and using it as

SmC* s1 = new(WORK_HEAD) SmC;
operator delete (s1,WORK_HEAD) ;

But the problem is overloaded delete operator is not calling the
destructor of class SmC.
Do I have to call the destructor explicitly?
Any suggestions please.

Feb 7 '07 #1
9 8170
On 6 Feb 2007 18:11:37 -0800, ro*******@gmail .com wrote:
>I have an overload delete operator as below

//////////////////////////////////
void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetM emoryHead(head_ type);
mmFree(&local_H ead,(char *)mem);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_ty pe);
}
/////////////////////
void* operator new(size_t sz, int head_Type) {
char *mem;
mmHead local_Head = CPRMemory::GetM emoryHead(head_ Type);
mem = mmAlloc(&local_ Head,sz);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_Ty pe);
if(!mem) cout<<"Out of Memory"<<endl;
return mem;
}
////////////////
and using it as

SmC* s1 = new(WORK_HEAD) SmC;
operator delete (s1,WORK_HEAD) ;

But the problem is overloaded delete operator is not calling the
destructor of class SmC.
Do I have to call the destructor explicitly?
Any suggestions please.
You've overridden placement delete, which is used only to deallocate memory in
case a constructor throws an exception. The destructor is not called if you
call placement delete.

What you need to do is to override placement new, placement delete, _and_
normal delete:
// Placement new
void* operator new(size_t sz, int) { /* ... */ }

// Placement delete
void operator delete(void*, int) { /* ... */ } // Note: no size_t parameter!

// Normal delete
void operator delete(void*) { /* ... */ }

then

SmC* s1 = new(WORK_HEAD) SmC; // Calls placement new
delete s1; // Calls normal delete, destructor called first.
If the constructor for SmC throws an exception, the placement delete function
will be called.

-dr
Feb 7 '07 #2
On 6 Feb 2007 18:11:37 -0800, ro*******@gmail .com wrote:
>I have an overload delete operator as below

//////////////////////////////////
void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetM emoryHead(head_ type);
mmFree(&local_H ead,(char *)mem);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_ty pe);
}
/////////////////////
void* operator new(size_t sz, int head_Type) {
char *mem;
mmHead local_Head = CPRMemory::GetM emoryHead(head_ Type);
mem = mmAlloc(&local_ Head,sz);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_Ty pe);
if(!mem) cout<<"Out of Memory"<<endl;
return mem;
}
////////////////
and using it as

SmC* s1 = new(WORK_HEAD) SmC;
operator delete (s1,WORK_HEAD) ;

But the problem is overloaded delete operator is not calling the
destructor of class SmC.
Do I have to call the destructor explicitly?
Any suggestions please.
By the way, you should follow convention when overriding operator new:

void* operator new(size_t, int)
{
while (true)
{
if ( /* allocation successful? */ )
{
return /* ptr to memory */
}

std::new_handle r handler = std::set_new_ha ndler(0);
std::set_new_ha ndler(handler);

if (handler)
{
(*handler)();
}
else
{
throw std::bad_alloc( );
}
}
}
Feb 7 '07 #3
On Feb 7, 3:46 pm, Dave Rahardja <a...@me.comwro te:
On 6 Feb 2007 18:11:37 -0800, rohits...@gmail .com wrote:
I have an overload delete operator as below
//////////////////////////////////
void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetM emoryHead(head_ type);
mmFree(&local_H ead,(char *)mem);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_ty pe);
}
/////////////////////
void* operator new(size_t sz, int head_Type) {
char *mem;
mmHead local_Head = CPRMemory::GetM emoryHead(head_ Type);
mem = mmAlloc(&local_ Head,sz);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_Ty pe);
if(!mem) cout<<"Out of Memory"<<endl;
return mem;
}
////////////////
and using it as
SmC* s1 = new(WORK_HEAD) SmC;
operator delete (s1,WORK_HEAD) ;
But the problem is overloaded delete operator is not calling the
destructor of class SmC.
Do I have to call the destructor explicitly?
Any suggestions please.

You've overridden placement delete, which is used only to deallocate memory in
case a constructor throws an exception. The destructor is not called if you
call placement delete.

What you need to do is to override placement new, placement delete, _and_
normal delete:

// Placement new
void* operator new(size_t sz, int) { /* ... */ }

// Placement delete
void operator delete(void*, int) { /* ... */ } // Note: no size_t parameter!

// Normal delete
void operator delete(void*) { /* ... */ }

then

SmC* s1 = new(WORK_HEAD) SmC; // Calls placement new
delete s1; // Calls normal delete, destructor called first.

If the constructor for SmC throws an exception, the placement delete function
will be called.

-dr
HI Dave ,

I am trying for memory optimization .
I have a chunk of memory which will be divided in to smaller pools of
memory .
So, my overloaded new will allocate memory from a particular pool.
Now during delete I need to know from which pool I need to free the
memory.
Due to this I need to use placement delete so that I can pass the pool
related info to the
overloaded delete. But I want to delete to call destructor also.

Allocation and freeing here means getting the memory from a linked
list of free nodes and adding it to linked list of allocated
node .Deleting means adding the node back to the link list.
These two lists are maintained inside a pool.

Feb 7 '07 #4
On Feb 7, 8:13 am, rohits...@gmail .com wrote:
On Feb 7, 3:46 pm, Dave Rahardja <a...@me.comwro te:
On 6 Feb 2007 18:11:37 -0800, rohits...@gmail .com wrote:
>I have an overload delete operator as below
>//////////////////////////////////
>void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetM emoryHead(head_ type);
mmFree(&local_H ead,(char *)mem);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_ty pe);
>}
>/////////////////////
>void* operator new(size_t sz, int head_Type) {
char *mem;
mmHead local_Head = CPRMemory::GetM emoryHead(head_ Type);
mem = mmAlloc(&local_ Head,sz);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_Ty pe);
if(!mem) cout<<"Out of Memory"<<endl;
return mem;
>}
>////////////////
>and using it as
SmC* s1 = new(WORK_HEAD) SmC;
>operator delete (s1,WORK_HEAD) ;
>But the problem is overloaded delete operator is not calling the
>destructor of class SmC.
>Do I have to call the destructor explicitly?
>Any suggestions please.
You've overridden placement delete, which is used only to deallocate memory in
case a constructor throws an exception. The destructor is not called ifyou
call placement delete.
What you need to do is to override placement new, placement delete, _and_
normal delete:
// Placement new
void* operator new(size_t sz, int) { /* ... */ }
// Placement delete
void operator delete(void*, int) { /* ... */ } // Note: no size_t parameter!
// Normal delete
void operator delete(void*) { /* ... */ }
then
SmC* s1 = new(WORK_HEAD) SmC; // Calls placement new
delete s1; // Calls normal delete, destructor calledfirst.
If the constructor for SmC throws an exception, the placement delete function
will be called.
-dr

HI Dave ,

I am trying for memory optimization .
I have a chunk of memory which will be divided in to smaller pools of
memory .
So, my overloaded new will allocate memory from a particular pool.
Now during delete I need to know from which pool I need to free the
memory.
Due to this I need to use placement delete so that I can pass the pool
related info to the
overloaded delete. But I want to delete to call destructor also.

Allocation and freeing here means getting the memory from a linked
list of free nodes and adding it to linked list of allocated
node .Deleting means adding the node back to the link list.
These two lists are maintained inside a pool.
If each pool is a contiguous ranges of memory then you can figure out
which pool an object has been allocated in by checking the address
against the ranges.

--
Erik Wikström

Feb 7 '07 #5

ro*******@gmail .com wrote:
On Feb 7, 3:46 pm, Dave Rahardja <a...@me.comwro te:
On 6 Feb 2007 18:11:37 -0800, rohits...@gmail .com wrote:
>I have an overload delete operator as below
>//////////////////////////////////
>void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetM emoryHead(head_ type);
mmFree(&local_H ead,(char *)mem);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_ty pe);
>}
>/////////////////////
>void* operator new(size_t sz, int head_Type) {
char *mem;
mmHead local_Head = CPRMemory::GetM emoryHead(head_ Type);
mem = mmAlloc(&local_ Head,sz);
CPRMemory::SetM emoryHeadAs(loc al_Head,head_Ty pe);
if(!mem) cout<<"Out of Memory"<<endl;
return mem;
>}
>////////////////
>and using it as
SmC* s1 = new(WORK_HEAD) SmC;
>operator delete (s1,WORK_HEAD) ;
>But the problem is overloaded delete operator is not calling the
>destructor of class SmC.
>Do I have to call the destructor explicitly?
>Any suggestions please.
You've overridden placement delete, which is used only to deallocate memory in
case a constructor throws an exception. The destructor is not called if you
call placement delete.

What you need to do is to override placement new, placement delete, _and_
normal delete:

// Placement new
void* operator new(size_t sz, int) { /* ... */ }

// Placement delete
void operator delete(void*, int) { /* ... */ } // Note: no size_t parameter!

// Normal delete
void operator delete(void*) { /* ... */ }

then

SmC* s1 = new(WORK_HEAD) SmC; // Calls placement new
delete s1; // Calls normal delete, destructor called first.

If the constructor for SmC throws an exception, the placement delete function
will be called.

-dr

HI Dave ,

I am trying for memory optimization .
I have a chunk of memory which will be divided in to smaller pools of
memory .
So, my overloaded new will allocate memory from a particular pool.
Now during delete I need to know from which pool I need to free the
memory.
Due to this I need to use placement delete so that I can pass the pool
related info to the
overloaded delete. But I want to delete to call destructor also.

Allocation and freeing here means getting the memory from a linked
list of free nodes and adding it to linked list of allocated
node .Deleting means adding the node back to the link list.
These two lists are maintained inside a pool.
Instead of using
operator delete (s1,WORK_HEAD) ;

call
delete s1;

That should call both the destructor and the overloaded delete. You
cannot pass any extra arguments to the destructor ( means also to your
delete function ).

However, be aware of inheritance nightmares while overloading new and
delete.
Also, instead of cluttering your class with memory allocation and
deallocation code, see if you can separate it to another class.

Feb 7 '07 #6
ro*******@gmail .com wrote:
...
and using it as

SmC* s1 = new(WORK_HEAD) SmC;
operator delete (s1,WORK_HEAD) ;

But the problem is overloaded delete operator is not calling the
destructor of class SmC.
'operator delete' is a raw memory deallocation function. It _never_ calls the
destructor and it's not supposed to do so.

When you destroy an object with delete-expression, which normally looks as follows

delete s1;

This is a delete-expression. It will do two things: 1) invoke the object's
destructor, 2) deallocate memory by selecting and calling 'operator delete'
function.

Note that 'operator delete' implements just a part of the functionality of
delete-expression. It is called when the destructor has already been called.
Do I have to call the destructor explicitly?
In your code you are not using delete-expression at all. This is fine, but that
means that you have to perform both of the aforementioned steps manually. I.e.
first you have to call the destructor manually and then you can deallocate the
memory by calling 'operator delete':

s1->SmC::~SmC();
operator delete(s1, WORK_HEAD);

--
Best regards,
Andrey Tarasevich
Feb 7 '07 #7
Dave Rahardja wrote:
You've overridden placement delete, which is used only to deallocate memory in
case a constructor throws an exception. The destructor is not called if you
call placement delete.
It's got squat to do with it being placement.

operator delete() is NOT the implementation of the delete operator
(despite it's unfortunately name), it is the memory deallocation
function that the delete operator uses.

Calling the deallocation function DOES not call the desructors
EVER, regardless of whether it is the normal or placement
version.
Feb 8 '07 #8
On Thu, 08 Feb 2007 07:28:53 -0500, Ron Natalie <ro*@spamcop.ne twrote:
>Dave Rahardja wrote:
>You've overridden placement delete, which is used only to deallocate memory in
case a constructor throws an exception. The destructor is not called if you
call placement delete.
It's got squat to do with it being placement.

operator delete() is NOT the implementation of the delete operator
(despite it's unfortunately name), it is the memory deallocation
function that the delete operator uses.

Calling the deallocation function DOES not call the desructors
EVER, regardless of whether it is the normal or placement
version.
You're right. What I meant was that the standard delete expression looks an
awful lot like a call to placement delete without the extra arguments.

-dr
Feb 9 '07 #9
On 6 Feb 2007 23:13:02 -0800, ro*******@gmail .com wrote:
>HI Dave ,

I am trying for memory optimization .
I have a chunk of memory which will be divided in to smaller pools of
memory .
So, my overloaded new will allocate memory from a particular pool.
Now during delete I need to know from which pool I need to free the
memory.
Due to this I need to use placement delete so that I can pass the pool
related info to the
overloaded delete. But I want to delete to call destructor also.

Allocation and freeing here means getting the memory from a linked
list of free nodes and adding it to linked list of allocated
node .Deleting means adding the node back to the link list.
These two lists are maintained inside a pool.
You've hit upon a classic design problem with C++ and memory allocation: The
fact that it's difficult to tell via during deletion how the memory was
allocated.

There are several solutions to the problem, but none of them are clean or
simple. Check out chapter 8 of Scott Meyers' Effective C++ (3rd Ed) for a
detailed look at the problem of customizing new and delete. Here are some
suggestions.

You can override new and delete for a class. This approach entangles the
problem domain of memory management with the problem domain your class was
intended to address.

You can override new and delete for a class, and then templatize the class to
use a memory manager class. This way the user of the class can choose (via the
template parameter) which memory manager will be used.

You can create a Factory that associates a class with a memory manager. You
call functions in the Factory (such as create() and destroy()) to perform
memory allocation and deallocation. In this scenario, the class is completely
disentangled from the memory allocation algorithm. However, you can no longer
use delete to destroy your class, or pass the pointers to other algorithms
that can delete your class for you.

You can override global new and delete. Each time new is called, allocate
memory that is slightly larger than needed. In the "spare" area of allocated
memory, store a pointer to the pool that you allocated the object from. During
deletion, peek at the pointer you stashed away to discover where the allocated
block came from. The downside to this is that your custom global new and
delete may be called to allocate _any_ class. It is difficult to restrict
allocation to a certain class or classes that are causing a bottleneck.

There are other solutions as well, but none of them are any cleaner or easier
to use.

-dr
Feb 9 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
27017
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object will call it's destructor when it is made a target of a delete".
4
1823
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a memory problem that I'm not readily seeing that lies within the overloaded operator - I think pertaining to the temporary class object that is created and used to return a value of the operands and the pointer variable of the class. Could someone...
20
37387
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The C++ Programming Language_, section 11.2 (page 263), these three operators 'take a name, rather than a value, as their second operand and provide the primary means of referring to members. Allowing them to be overloaded would lead to subtleties.'"...
2
1685
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you can see in the constructor. Class Matris should create a matris by using class Intvektor. So what I want to have is a pointer to an array of Intvektor and in each positionindex in this array will I have a pointer to an array of Intvektor
10
2247
by: MaxMax | last post by:
int *p; p = new int; void *q = (void*)p; delete q; Is this valid if p is guaranteed to be a POD? (don't ask why... there is a reason I need this. Clearly I will create p in function, use p in another function that know what type p is and delete p in a function that for simplicity won't know what p is)
6
1886
by: mdinino | last post by:
Hi, I am new to C++, and I have a simple question but I can't seem to find a direct answer. If I use new to create something in the scope of a function and then I return the pointer that I get to another function, do I need to worry about the memory space being used by anything else and causing the program to crash?
13
5033
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one implement operator new/delete I can't see a way to indicate, on delete, how many objects must be destroyed (or how big the space is) - alternatively I can't figure out what are the alignment requirements so that the implementation, after calling my...
9
2054
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded the + operator by means of a friend function. Everything worked fine until I decided to use a variable array size (by using new/delete), now I get an error when a temporary object is deleted (e.g. after addition), the error occurs at the delete...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.