[C++-sig] Allocating objects on the heap by default.

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Sat Jul 5 05:52:35 CEST 2003


Hi,

I have a question that I'm not sure has much to do with Boost.Python
itself but I'm wrapping some code via Boost.Python and need to know
what the best approach is to solve the problem.

The code is designed to be used by allocating objects on the heap (via
new) and handing them off to very simple containers that manage these
objects.  The lifetime of the objects is also managed by the
container.  So here is a quick example.

// --------------------
class Obj {
public:
    Obj();
    ~Obj();
};

class Mgr {
public:
    Mgr();
    ~Mgr(); // deletes memory for objects in container.
    void add(Obj* a);
    void popBack();
private:
    std::vector<Obj*> arr;
};

void usage()
{
    Obj *o = new Obj();
    Mgr *m = new Mgr();
    m->add(o);
    delete m;
}
// --------------------

Perhaps the design is totally stupid, but its used consistently
everywhere and quite simple once you know you can't delete an object
you created and passed to a manager.  Juas as the user has to manage
and remember to invoke delete for every new he has to remember that
the manager manages memory as well.  I don't see that as a bad
decision but I guess purists would not agree with me.  Anyway, I want
to wrap the code above (except the usage function) and want to arrange
for Boost.Python to allocate all of the objects, Obj and Mgr in the
heap by default.  After wrapping the above via Boost.Python and using
it from Python this is what happens:

>>> import test
>>> a = test.Obj()
>>> b = test.Mgr()
>>> b.add(a)
>>> del b
Segmentation fault.

Trouble here is that Obj is not allocated on the heap but on the
stack, just as one would expect in C++.  Yes, I read the penultimate
FAQ entry and my question is: can I arrange the wrappers to
automatically allocate Obj and Mgr objects on the heap and
additionally be handled by a shared_ptr/auto_ptr without having to
write a factory function like NewObj for every single class?  I'd like
to avoid writing factory functions just to do this since I have lots
of classes like this.  If the object is allocated on the heap by
default and I manage the object via boost::shared_ptr then the above
segfault will not happen.  Is there a way that this can be handled
easily?  Alternatively can this be generated via Pyste/Python magic?

FWIW and FYI, not that it matters but SWIG does this by default --
allocates everything via new and deals with the resulting opaque
pointers.

Also, is the above approach of creating objects and handing them off
to a manager a bad thing to do in general?  Whats a better approach?
This is a generic c++ question but the issue arises in the context of
wrapping so I thought it sufficient relevant to post here and not a
c++ list.  I'm sorry if this is inappropriate.

Any suggestions will be much appreciated.

Thanks!

cheers,
prabhu






More information about the Cplusplus-sig mailing list