473,769 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom destructors -- memory management

I need to design a container that must hold a set of references to a variety
of object types, managed by different memory managers. At some time, the
container must destroy each object. Currently I have the following design in
mind:
class Destructible // interface
{
public:
virtual void destroy() = 0;
};
class Container
{
public:
// ...

void add(Destructibl e& item);

// ...
void destroy();
};

void Container::dest roy()
{
// ... iterate ...
items[i].destroy();
}
// An item in the container
class Item: virtual public Destructible
{
public:
virtual void destroy();
};

void Item::destroy()
{
MyMemoryManager ::Lock lock(this);
~Item();
}
Is there an easier way to do this? Is there a way to make the container work
with objects without a custom destructor?

-dr
Apr 3 '07 #1
6 2133
On 3 Apr, 07:45, Dave Rahardja
<drahardja_atsi gn_pobox_dot_.. .@pobox.comwrot e:
I need to design a container that must hold a set of references to a variety
of object types, managed by different memory managers. At some time, the
container must destroy each object. Currently I have the following designin
mind:

class Destructible // interface
{
public:
virtual void destroy() = 0;

};

class Container
{
public:
// ...

void add(Destructibl e& item);

// ...
void destroy();

};

void Container::dest roy()
{
// ... iterate ...
items[i].destroy();

}

// An item in the container
class Item: virtual public Destructible
{
public:
virtual void destroy();

};

void Item::destroy()
{
MyMemoryManager ::Lock lock(this);
~Item();

}

Is there an easier way to do this? Is there a way to make the container work
with objects without a custom destructor?
Maybe I missed some of the requirements but what's wrong with just
using the normal destructor? If the container uses any of the standard
containers then the destructor will be called automatically. If you
are using pointers then the destructor will be called on delete.

Have you perhaps been using some garbagecollecte d language like Java
or C# where you don't know when an object is destroyed? Well, in C++
you do, so all you have to worry about is to make sure that the object
is destroyed (going out of scope or using delete on a pointer) and
that the destructor cleans up the stuff that needs to be cleaned up.

--
Erik Wikström

Apr 3 '07 #2
wo
On Apr 3, 7:45 am, Dave Rahardja
<drahardja_atsi gn_pobox_dot_.. .@pobox.comwrot e:
// ... iterate ...
items[i].destroy();
Have you ever considered using iterators ? You might want to google
for "C++ iterator" in order to do such traversal. items[i] is the C
way of seeing the problem, which isn't really the best one for
containers where random access (using the [] operator) isn't
available, like lists.
Apr 3 '07 #3
On 2 Apr 2007 23:36:11 -0700, "Erik Wikström" <er****@student .chalmers.se>
wrote:
>On 3 Apr, 07:45, Dave Rahardja
<drahardja_ats ign_pobox_dot_. ..@pobox.comwro te:
>I need to design a container that must hold a set of references to a variety
of object types, managed by different memory managers. At some time, the
container must destroy each object. Currently I have the following design in
mind:

Maybe I missed some of the requirements but what's wrong with just
using the normal destructor? If the container uses any of the standard
containers then the destructor will be called automatically. If you
are using pointers then the destructor will be called on delete.

Have you perhaps been using some garbagecollecte d language like Java
or C# where you don't know when an object is destroyed? Well, in C++
you do, so all you have to worry about is to make sure that the object
is destroyed (going out of scope or using delete on a pointer) and
that the destructor cleans up the stuff that needs to be cleaned up.
I'm sorry if I wasn't clear--it wasn't the destructor call that I was worried
about. It is that each of the elements in this container may be allocated by a
_different_ memory manager. Thus, using global delete may not be appropriate
for all elements.

-dr
Apr 3 '07 #4
On 3 Apr 2007 03:55:18 -0700, "wo" <dr******@gmail .comwrote:
>On Apr 3, 7:45 am, Dave Rahardja
<drahardja_ats ign_pobox_dot_. ..@pobox.comwro te:
> // ... iterate ...
items[i].destroy();

Have you ever considered using iterators ? You might want to google
for "C++ iterator" in order to do such traversal. items[i] is the C
way of seeing the problem, which isn't really the best one for
containers where random access (using the [] operator) isn't
available, like lists.
Since I am _providing_ the container, I think I should be free to use whatever
traversal method I like internally! ;-)

Joking aside, the traversal is not the question here. I merely used the []
notation to suggest that there is some sort of iteration going on internally
when the objects are destroyed.

The question I'm asking is about how to destroy these objects, which may have
been allocated on a variety of memory managers.

-dr
Apr 3 '07 #5
On Apr 3, 3:07 pm, Dave Rahardja
<drahardja_atsi gn_pobox_dot_.. .@pobox.comwrot e:
On 2 Apr 2007 23:36:11 -0700, "Erik Wikström" <eri...@student .chalmers.se>
wrote:
On 3 Apr, 07:45, Dave Rahardja
<drahardja_atsi gn_pobox_dot_.. .@pobox.comwrot e:
I need to design a container that must hold a set of
references to a variety of object types, managed by
different memory managers. At some time, the container must
destroy each object. Currently I have the following design
in mind:
Maybe I missed some of the requirements but what's wrong with just
using the normal destructor? If the container uses any of the standard
containers then the destructor will be called automatically. If you
are using pointers then the destructor will be called on delete.
Have you perhaps been using some garbagecollecte d language like Java
or C# where you don't know when an object is destroyed? Well, in C++
you do, so all you have to worry about is to make sure that the object
is destroyed (going out of scope or using delete on a pointer) and
that the destructor cleans up the stuff that needs to be cleaned up.
I'm sorry if I wasn't clear--it wasn't the destructor call
that I was worried about. It is that each of the elements in
this container may be allocated by a _different_ memory
manager. Thus, using global delete may not be appropriate for
all elements.
How do you know which memory manager is responsible for a given
object? If it is purely a function of the type, then using a
common base class, and providing an operator delete member
function in the class should do the trick; you just use delete,
as normal, and the compiler takes care of the rest. Failing
that, you need some means of tracking which memory manager is
responsible for which bit of memory. The most frequent solution
for this is to use some hidden memory in front of the allocated
block. You then replace the global new and delete functions to
set this memory, and to use it when they are called. Say
something along the lines of:

union BlockHeader
{
MemoryManager* owner ;
double dummyForAlignme nt ; // may need something more
// for some processors...
} ;

void* operator new( size_t n )
{
BlockHeader* result =
static_cast< BlockHeader * >(
::malloc( n + sizeof( BlockHeader ) ) ) ;
if ( result == NULL ) {
throw std::bad_alloc( ) ;
}
result->owner = NULL ;
return result + 1 ;
}

void* operator new( size_t n, MemoryManager* from )
{
BlockHeader* result =
static_cast< BlockHeader * >(
from->alloc( n + sizeof( BlockHeader ) ) ) ;
if ( result == NULL ) {
throw std::bad_alloc( ) ;
}
result->owner = from ;
return result + 1 ;
}

void operator delete( void* p )
{
BlockHeader* block
= static_cast< BlockHeader* >( p ) - 1 ;
if ( block->owner == NULL ) {
::free( block ) ;
} else {
block->owner->free( block ) ;
}
} ;

Again, this takes care of everything automatically. You
allocate with new or new(memmgr), and you delete as usual.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 3 '07 #6
On 3 Apr 2007 07:50:12 -0700, "James Kanze" <ja*********@gm ail.comwrote:
>How do you know which memory manager is responsible for a given
object? If it is purely a function of the type, then using a
common base class, and providing an operator delete member
function in the class should do the trick; you just use delete,
as normal, and the compiler takes care of the rest. Failing
that, you need some means of tracking which memory manager is
responsible for which bit of memory. The most frequent solution
for this is to use some hidden memory in front of the allocated
block. You then replace the global new and delete functions to
set this memory, and to use it when they are called. Say
something along the lines of:
>
Again, this takes care of everything automatically. You
allocate with new or new(memmgr), and you delete as usual.
Hey that's a great idea! That might just be the ticket.

-dr
Apr 4 '07 #7

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

Similar topics

6
2185
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does not use garbage collection (as Java does). So if the script runs a loop: for i in range(100): f = Obj(i)
8
1430
by: johny smith | last post by:
If I have a simple class with say a couple of integers only is there any need for me to provide a destructor? thanks!
7
1948
by: Luc Tremblay | last post by:
Given the typical following code: void Listener::HandleEvent(const Event& event) { // handling code } In a "clean" fashion, how is it possible to add custom data (to be subsequently accessed) to the Event instance? By custom data i mean practically anything, from a class to a single int. Particularly to my case,
12
2584
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in advance, Joe
3
2068
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual destructor? When I am defining such a class with default destructor then my compiler is giving warning that class XXX has virtual functions but non-virtual destructor.
6
2255
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes contains a pointer to int values as a filed. In the definition (implementation) of constructor I use this pointer to create table of int values with the new operator. The number of elements of the table is provided by the user during execution of the...
2
2020
by: Wiktor Zychla [C# MVP] | last post by:
suppose I implement a custom caching mechanism for my custom ORM implementation. suppose I'd like the client code to be able to define the maximum amount of memory the cache is allowed to occupy. however, I have no idea how from within the code I can even retrieve the amount of memory an object takes. the System.GC.GetTotalMemory method seem to retrieve the total amount of allocated memory while I would be interested in monitoring the...
5
24789
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
1
1774
by: ebony.soft | last post by:
Dear all Hi As you know constructor is a member function with several missions and one of them is "acquiring a resource" and in the same token destructor "releases the resource". Usually after such descriptions, It is said, the resource is like memory, file, lock, semaphore, ... As a matter of fact, resource isn't confined to memory and constructor/destructor do more than just memory management. I reviewed most of the major books and...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
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
9863
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
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
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
5304
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.