1

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!

1
  • use std::shared_ptr or std::unique_ptr for reference counting. Commented Nov 17, 2012 at 0:42

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.