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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:28:27+00:00 2026-06-13T05:28:27+00:00

Let’s say I have a simple C++ class: class Widget { public: Widget() :

  • 0

Let’s say I have a simple C++ class:

class Widget
{
public:
    Widget() :
            x(1)
    { }

    void sleep()
    {
        sleep(x);
    }

private:
    int x;
};

Widget::sleep blocks, so I would like to free up the GIL so that Python can do some other stuff, so I wrapped Widget::sleep in a function like this:

static void pyWidgetSleep(Widget& self)
{
    PyThreadState* state = PyEval_SaveThread();
    self.sleep();
    PyEval_RestoreThread(state);
}

For completeness, the binding code looks like this:

BOOST_PYTHON_MODULE(libnative)
{
    boost::python::class_<Widget>("Widget")
        .def("sleep", &pyWidgetSleep);
}

I have a simple Python script that looks like this:

import libnative
import threading

class C:
    def __init__(self):
        self.x = libnative.Widget()
        self.sleeping = False

    def foo(self):
        self.sleeping = True
        self.x.sleep()

c = C()
t = threading.Thread(target=c.foo)
t.start()
while not c.sleeping:
    pass
del c.x

Do all participants in an expression get their ref count incremented for the duration of the expression?
The only referant to c.x when used in C.foo is c, which the line after t.start() conveniently deletes.
Since pyWidgetSleep releases the GIL, del c.x might decrement the last reference to the Widget instance, causing undefined behavior.


I can’t make this break on my machine, which seems to be a good indication that it works as I would expect, but reference-counting isn’t documented to this degree of clarity
(or, at the very least, I can’t seem to find it). The relevant portion of CPython appears to be PyEval_EvalCodeEx, which looks like it applies Py_INCREF to all arguments
involved in a function call, but I could be totally off on that one.

  • 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-13T05:28:28+00:00Added an answer on June 13, 2026 at 5:28 am

    The answer to this question is yes, it is safe. This happens as a consequence of how member functions are called in Python. Consider if Widget was implemented in Python:

    class Widget:
        def __init__(self):
            self.x = 1
    
        def sleep(self):
            os.sleep(self.x)
    

    Notice how the first parameter to Widget.sleep is self? The reference count is incremented for the duration of the call! It seems obvious in retrospect…

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

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and
Let me explain best with an example. Say you have node class that can
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say, I have some user input (as a String) like 11010011011. Now I
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let's say I have a simple form in the administration of my Rails 2.x

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.