Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 957195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:39:46+00:00 2026-05-16T00:39:46+00:00

I have a library which creates objects (instances of class A) and pass them

  • 0

I have a library which creates objects (instances of class A) and pass them to a python program which should be able to call their methods.

Basically I have C++ class instances and I want to use them from python. Occasionally that object should be passed back to C++ for some manipulations.

I created the following wrapper file (let’s assume that the New function is called somewhere in the C++ code):

#include <boost/python.hpp>
#include <iostream>
#include <boost/smart_ptr.hpp>

using namespace boost;
using namespace boost::python;

int calls = 0;

struct A
{
   int f() { return calls++; }
   ~A() { std::cout << "destroyed\n"; }
};

shared_ptr<A> existing_instance;

void New() { existing_instance = shared_ptr<A>( new A() ); }

int Count( shared_ptr<A> a ) { return a.use_count(); }

BOOST_PYTHON_MODULE(libp)
{
    class_<A>("A")
        .def("f", &A::f)
    ;

    def("Count", &Count);

    register_ptr_to_python< shared_ptr<A> >();
} 

The code lacks the part where the python gets the existing_instance. I didn’t paste that, but let’s just say I use a callback mechanism for that purpose.

This code works but I have a few questions:

  1. In the Count function (and in all other C++ manipulation functions) is it fine to pass a like that or it’s better to do something like const shared_ptr<A>&? In the code snippets I found in the python boost documentation the reference is often used but I don’t understand the difference (apart from having a higher reference counter, of course).

  2. Is this code “safe”? When I pass the existing_instance to python, its counter will be incremented (just once, even if in python I make more copies of the object, of course) so there is no way that the C++ code could destroy the object as far as python holds at least a “copy”. Am I correct? I tried to play with pointers and it seems I’m correct, I’m asking just to be sure.

  3. I’d like to prevent python from creating instances of A. They should only be passed from C++ code. How could I achieve that? EDIT: found, I just need to use no_init and noncopyable: class_<A, boost::noncopyable>("A", no_init)

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:39:47+00:00Added an answer on May 16, 2026 at 12:39 am

    boost::python knows all about boost::shared_ptr, but you need to tell it that boost::shared_ptr<A> holds an instance of A, you do this by adding boost::shared_ptr<A> in the template argument list to class_, more information on this ‘Held Type’ is here in the boost documentation.

    To prevent instances being created from python, you add boost::python::no_init to the class_ constructor, so you end up with:

    boost::python::class_< A, boost::shared_ptr<A> >("A", boost::python::no_init)
        //... .def, etc
        ;
    

    In general you should not pass around shared pointers by reference, since if the reference to the shared pointer is invalidated, then the reference to which the shared pointer is pointing to is also invalidated (since taking a reference of the shared pointer didn’t increment the reference counter to the pointed to object).

    It is perfectly safe to pass boost::shared_ptr objects around to and from python, reference counts (python and shared_ptr) will be correctly managed provided you don’t change the return_value_policy. If you change the policy of a method exposed in python so that it returns a reference to a shared pointer then you can cause problems, just as passing shared pointers around by c++ references can cause problems.

    (Also, you should use make_shared<A>(...) in preference to shared_ptr<A>(new A(...)).)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a class library called AddServiceLibrary in which I have a method
I am using the apache library. I have created a class which sends a
I have a library which has a basic class which is used extensively by
I'm interacting with an API for which I have a Python library. The library
I have a WCF Web Service which is referenced from a class library. After
In my project's core library we have a very big class, which is tending
I have created a library which reads the app.config file and gets the type
I have this library called BASS which is an audio library which I'm going
If i have a library which is compiled in .NET 4 and reference it
I have a .dll library which exports a function in the following format: _Java_folder1_folder2_folder3_JavaClassName_javamethodname@16

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.