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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:02:09+00:00 2026-06-01T14:02:09+00:00

Is there a way to remove all references to an object at once? I

  • 0

Is there a way to remove all references to an object at once? I know that’s unpythonic, so I’ll explain what I’m trying to do and maybe someone knows a better way.

I’m writing an object-oriented wrapper around a SWIG wrapper for a C library. When a proxy for one of the C objects is deleted, it also deletes child objects (directly in C). I’d like that to also trigger deletion of their proxy objects in Python. Otherwise I run into a situation where there are Python objects carrying around invalid pointers which will segfault if they’re accessed.

It looks sort of like this:

class Parent(object):
    def __init__(self):
        self.ptr = swig.createParent()
    def __del__(self):
        swig.deleteParent(self.ptr) # also deletes children

class Child(object):
    def __init__(self, parent):
        self.ptr = swig.createChild(parent)
    def __del__(self):
        swig.deleteChild(self.ptr)

And this is the situation I’m worried about:

p = Parent()
c = Child(parent)
del p
# accessing c.ptr now would be bad right?
  • 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-01T14:02:10+00:00Added an answer on June 1, 2026 at 2:02 pm

    If I understand you correctly, you are wrapping some C code, and the C code has a destructor that can be called. After that, any attempt to use the pointer to the C code object causes a fatal crash.

    I am not sure of your exact situation, so I am going to give you two alternate answers.

    0) If the C object can be freed for some reason out of your control, and you need to make sure your Python wrapper code doesn’t crash, you need to make the Python wrapper know whether the C object is available or not. Make your Python object handle the pointer no longer being valid. You could raise a Python exception, return an error code, or just have the method functions become no-op functions, depending on what you are doing. The C object going away doesn’t free the Python object, so you can handle this cleanly.

    1) If the C object is only freed when the Python object is freed, you don’t have a problem. Python references, when they go out of scope or you call del() on them, do not free the Python object; they just decrement the reference count on that object. When the reference count goes to zero, then the object is freed and your __del__() method function can call into the C code to free the C object.

    You can watch how it works by running this code:

    class DelTest(object):
        def __init__(self):
            print "__init__() called: object %08x created" % id(self)
        def __del__(self):
            print "__del__() called: object %08x destroyed" % id(self)
    
    print "begin"
    print "creating object, binding to name d"
    d = DelTest()
    print "adding reference bound to name x"
    x = d
    print "adding reference bound to lst[0]"
    lst = []
    lst.append(d)
    print "deleting lst"
    del(lst)
    print "deleting x"
    del(x)
    print "deleting d"
    del(d)
    print "end"
    

    Output from the above:

    begin
    creating object, binding to name d
    __init__() called: object 01e4db50 created
    adding reference bound to name x
    adding reference bound to lst[0]
    deleting lst
    deleting x
    deleting d
    __del__() called: object 01e4db50 destroyed
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to remove all forgeinKey and primary key contriants from Database
Is there any way in VB.NET to remove all of the whitespaces between tags
is there any way to remove an attachment from Zend_Mail object? Simple example: $mail
Is there a way that I can pass in a reference to an object,
If learned the hard way that you should remove the delegate from an object
Is there any way to remove the dotted line rectangle, which indicates the keyboard
Is there a way to remove an item from a listbox based on a
Is there a way to remove duplicate (key-value) pairs from NSDictionary ? EDIT: My
Is there a way to remove the grid header rollover in flex while still
Is there any way to remove the contents of an file in php, do

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.