469,945 Members | 1,348 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

Standard containers, polymorphic objects, and memory management.

I'm trying decide on the best way to structure the memory management in
my program.

I have a class (lets call it World), which contains a collection of
Entity objects.

Entity in turn, contains a pointer a Shape object. Shape is a pure
virtual class.

Now, I'm not sure who should "own" the allocation and deallocation of
the Shapes. It makes sense that World should own the Entity objects
directly, but it seems to complicate matters if I want the Entity
objects to delete (through auto_ptr, or on there own destructor) the
Shape *.

Also, I'd prefer (although its not a requirement) to avoid the "new"
operator for a lot of this. eg, I'd like to do something like this:

World world;
world.addEntity(Entity(new Sphere(sphereParams)));

where you have:
void World::addEntity(Entity &entity) {
entities.push_back(entity);
}

Now, if Entity deletes its shape on destruction, I need to make sure
that the Entity copy constructor uses move semantics, but I'm not sure
that's compatible with std::vector<Entity>. Or I need to make World
accept (and manage) Entity pointers.

I suppose I could also make it so that World manages the Shape object
life-cycle, eg "Shape &shape World::addShape(Shape *shape);".

So, any suggestions on which route I should take?

Thanks,
Daniel.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jun 27 '08 #1
2 1285
I have a class (lets call it World), which contains a collection of
Entity objects. Entity in turn, contains a pointer a Shape
object. Shape is a pure virtual class.
Your description looks like this:

class Shape
{
// one or more pure virtual functions

};

class Entity
{
std::auto_ptr<Shapes_; // or shared_ptr<Shape>
// ...

// has copy ctor & assignment op
// by either cloning or sharing the pointee.

};
class World
{
std::vector<Entityentities_;
// ...
};

So assuming you have a single World instance, memory management seems
to be taken care of.
Now, if Entity deletes its shape on destruction, I need to make sure
that the Entity copy constructor uses move semantics, but I'm not
sure that's compatible with std::vector<Entity>. Or I need to make
World accept (and manage) Entity pointers.
You can ensure that Shape has clear clone semantics or use shared_ptr
idiom to make it compatible to vector<Entity>. I would advise against
use of raw pointers.

-Amal
Jun 27 '08 #2
On Jun 3, 4:36 am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.netwrote:
I'm trying decide on the best way to structure the memory
management in my program.
I have a class (lets call it World), which contains a
collection of Entity objects.
Entity in turn, contains a pointer a Shape object. Shape is a
pure virtual class.
Now, I'm not sure who should "own" the allocation and
deallocation of the Shapes. It makes sense that World should
own the Entity objects directly, but it seems to complicate
matters if I want the Entity objects to delete (through
auto_ptr, or on there own destructor) the Shape *.
Be careful with the concept of ownership. In most applications,
it doesn't really make sense to have a "world" object which owns
the entity objects, but rather the let the entity objects "own"
themselves. And do Shape objects really require destruction; do
they have a deterministic lifetime. If not, then the simplest
solution is to use a garbage collector, and not worry about it.

Otherwise, I don't think that there's any real problem in having
Entity be responsible for the lifetime of its Shape. But of
course, this will really depend on the actual semantics of the
objects in question. There is no one rule which fits all cases.
Also, I'd prefer (although its not a requirement) to avoid the
"new" operator for a lot of this.
If you're objects are polymorphic, you don't have much choice.
eg, I'd like to do something like this:
World world;
world.addEntity(Entity(new Sphere(sphereParams)));
where you have:
void World::addEntity(Entity &entity) {
entities.push_back(entity);
}
Why? Entity objects typically have identity, which means that
they don't support copy or assignment; they usually also have an
arbitrary lifetime. Both of which mean that they should
probably never be allocated other than dynamically.

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gandalf | last post: by
43 posts views Thread by Steven T. Hatton | last post: by
4 posts views Thread by Vincenzo Cappello | last post: by
35 posts views Thread by dragoncoder | last post: by
21 posts views Thread by George Exarchakos | last post: by
2 posts views Thread by Arash Partow | last post: by
7 posts views Thread by mscava | last post: by
14 posts views Thread by Remo D. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.