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 9129757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:49:16+00:00 2026-06-17T07:49:16+00:00

In my program I manage references to python objects in C++. I.e. all my

  • 0

In my program I manage references to python objects in C++. I.e. all my classes are derived from class Referenced, which contains pointer to corresponding python object.

class Referenced
{
public:
    unsigned use_count() const
    { 
        return selfptr->ob_refcnt;
    }

    void add_ref() const
    {
        Py_INCREF(selfptr);
    }

    void remove_ref() const
    {
        Py_DECREF(selfptr);
    }

    PyObject* selfptr;
};

I use intrusive_ptr to hold objects derived from Referenced. This allows me to easily keep references to required python objects in C++ and access them whether necessary. But my program crashes (only in windows howewer) when python object is going to be deleted from C++, i.e. when I call Py_DECREF(selfptr), whether selfptr->ob_refcnt == 1. Is this approach OK?


Upd: I finally figured out problem in my program. It wasn’t related directly to object removal. To check the initial question I’ve implemented simple extension module remembering reference to python object and releasing it on demand. Here is it:

#include <Python.h>

static PyObject* myObj;

static PyObject* acquirePythonObject(PyObject* self, PyObject* obj)
{
    printf("trying to acquire python object %p, refcount = %d\n", obj, obj->ob_refcnt);
    myObj = obj;
    Py_INCREF(myObj);
    printf("reference acquired\n");
    return Py_True;
}

static PyObject* freePythonObject(PyObject*, PyObject*)
{
    printf("trying to free python object %p, refcount = %d\n", myObj, myObj->ob_refcnt);
    Py_DECREF(myObj);
    printf("reference removed\n");
    return Py_True;
}

static PyMethodDef moduleMethods[] =
{
    {"acquirePythonObject", acquirePythonObject, METH_O, "hold reference to python object."},
    {"freePythonObject", freePythonObject, METH_NOARGS, "free reference to python object."},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initmodule(void)
{
    Py_InitModule("module", moduleMethods);
}

And python script:

import module

class Foo:
    def __init__(self):
        print "Foo is created"

    def __deinit__(self):
        print "Foo is destroyed"

def acquireFoo():
    foo = Foo()
    module.acquirePythonObject(foo)

def freeFoo():
    module.freePythonObject()

if __name__ == "__main__":
    acquireFoo()
    freeFoo()

Sample runs seamlessly in windows and linux. Below is the output.

Foo is created
trying to acquire python object 0x7fa19fbefd40, refcount = 2
reference acquired
trying to free python object 0x7fa19fbefd40, refcount = 1
Foo is destoryed
reference removed
  • 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-06-17T07:49:18+00:00Added an answer on June 17, 2026 at 7:49 am

    Is this approach OK?

    Basically, but …

    • I don’t see any guarantee that add_ref/remove_ref are called the correct number of times (using RAII would automate this – maybe that’s what your intrusive_ptr does?)
    • if you do try to remove_ref too many times, I’m not sure what Python guarantees. If you set selfptr = NULL when you know the refcount is going from 1 -> 0, you could catch this
      • either by crashing hard, or by checking explicitly, or by using Py_XDECREF
      • even better, just use Py_CLEAR instead

    And finally … do you have any crash dump or diagnostic info?

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

Sidebar

Related Questions

I have this two tables from my program to manage stock: Articulos (items): id_articulo
Greetings, Forum. I'm working on a program in Python that uses Twisted to manage
I have the following line of code in my program which I took from
My program will create and delete a lot of objects (from a REST API).
I've made a program to manage a movie collection and it stores the data
I have a program that will let me manage users on our terminal server
In an embedded program I have a screen object that needs to manage a
Im using UITableViewController to manage data. When I click on the cell my program
For my class, I am making a program that manages a Hotel. My 'Test'
Is it possible, while debugging a Silverlight app, to view all current references to

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.