JLSM
JLSM

Reputation: 13

python::boost::object allocation

I was wondering if it's correct to dynamically allocate an object of type boost::python::object.

boost::python::object * obj = new boost::python::object();

Will there be any problems? What about the internal reference counting?

Thanks!

Upvotes: 1

Views: 250

Answers (1)

Josh Heitzman
Josh Heitzman

Reputation: 1834

The docs for boost::python::object explicitly state that the destructor decrements the reference count and the assignment operator increments it. While not explicitly stated, from those explicit statements and this statement about the consturctor Constructs an object managing a reference we can infer that the constructor increments the reference count as well.

Since new invokes the constructor, as long as delete is called on the pointer to ensure the destructor is called, there will not be any problems with the internal reference counting.

Also note that the comment about using std::shared_ptr or std::unique_ptr for reference counting is somewhat misleading. std::unique_ptr does not reference count but rather moving / move assigning causes ownership to be passed from instance A to instance B and instance A to become invalid. Additinally, std::shared_ptr will not modify the internal reference count that a boost::python::object maintains, but rather it will manage a separate reference count about the lifetime of the boost::python::object pointer.

Upvotes: 2

Related Questions