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

  • SEARCH
  • Home
  • 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 6229657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:36:35+00:00 2026-05-24T09:36:35+00:00

I’m using Boost.Python to expose my C++ code to Python. I’ve encountered a difficulty

  • 0

I’m using Boost.Python to expose my C++ code to Python. I’ve encountered a difficulty related to having an object passed from one language to the other multiple times. Here’s what I want to do:

The C++ code

class Base
{
  public:
    void baseTest() {
        std::cout << "Base::basetest()";
    }
};

class Deriv
    : public Base
{
  public:
    void derivTest() {
        std::cout << "Deriv::derivTest()";
    }
};

void call(Base& b, boost::python::object func)
{
    func(b);
}

BOOST_PYTHON_MODULE(event)
{
    using namespace boost;
    using namespace boost::python;

    class_<Base>("Base")
        .def("baseTest", &Base::baseTest)
    ;

    class_<Deriv, bases<Base>>("Deriv")
        .def("derivTest", &Deriv::derivTest)
    ;

    def("call", call);
}

Python code

from event import *

def callback(deriv):
    deriv.baseTest() # works fine
    deriv.derivTest() # crash!

def run():
    d = Deriv()
    call(d, callback)

What I want to happen

  1. C++: Calls the run() function defined in Python. (No problem here)

  2. Python: run() creates a new Deriv object; it passes it and a function object, callback, back to C++, through the call function. (Also okay)

  3. C++: call() takes the Deriv object as a Base&. It passes the Base-reference to the Python callback it received through the second parameter.

  4. Python: The callback receives the Base object from C++. However, it expects it to be a Deriv: if I call derivTest() the program crashes. However, if I call baseTest(), it doesn’t crash.

How can I make the callback not crash?

  • 1 1 Answer
  • 0 Views
  • 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-24T09:36:37+00:00Added an answer on May 24, 2026 at 9:36 am

    Thanks for the comments, I found the solution to this. It’s actually quite simple, you just have to wrap the Base object in a shared_ptr instead of passing it by reference, like this:

    void call(boost::shared_ptr<Base> b, boost::python::object func)
    {
        func(b);
    }
    

    But be careful about something. I tried to used std::shared_ptr that comes with Visual C++ 2010 Express (the ‘memory’ header) and it caused a crash. I had to use the boost::shared_ptr for it to work. (I’m using version 1.46.1 of Boost.)

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a bunch of posts stored in text files formatted in yaml/textile (from
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.