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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:31:43+00:00 2026-05-12T10:31:43+00:00

The problem is basically this, in python’s gobject and gtk bindings. Assume we have

  • 0

The problem is basically this, in python’s gobject and gtk bindings. Assume we have a class that binds to a signal when constructed:

class ClipboardMonitor (object):
  def __init__(self):
    clip = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD)
    clip.connect("owner-change", self._clipboard_changed)

The problem is now that, no instance of ClipboardMonitor will ever die. The gtk clipboard is an application-wide object, and connecting to it keeps a reference to the object, since we use the callback self._clipboard_changed.

I’m debating how to work around this using weak references (weakref module), but I have yet to come up with a plan. Anyone have an idea how to pass a callback to the signal registration, and have it behave like a weak reference (if the signal callback is called when the ClipboardMonitor instance is out of scope, it should be a no-op).

Addition: Phrased independently of GObject or GTK+:

How do you provide a callback method to an opaque object, with weakref semantics? If the connecting object goes out of scope, it should be deleted and the callback should act as a no-op; the connectee should not hold a reference to the connector.

To clarify: I explicitly want to avoid having to call a “destructor/finalizer” method

  • 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-12T10:31:44+00:00Added an answer on May 12, 2026 at 10:31 am

    The standard way is to disconnect the signal. This however needs to have a destructor-like method in your class, called explicitly by code which maintains your object. This is necessary, because otherwise you’ll get circular dependency.

    class ClipboardMonitor(object):
        [...]
    
        def __init__(self):
            self.clip = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD)
            self.signal_id = self.clip.connect("owner-change", self._clipboard_changed)
    
        def close(self):
            self.clip.disconnect(self.signal_id)
    

    As you pointed out, you need weakrefs if you want to avoid explicite destroying. I would write a weak callback factory, like:

    import weakref
    
    class CallbackWrapper(object):
        def __init__(self, sender, callback):
            self.weak_obj = weakref.ref(callback.im_self)
            self.weak_fun = weakref.ref(callback.im_func)
            self.sender = sender
            self.handle = None
    
        def __call__(self, *things):
            obj = self.weak_obj()
            fun = self.weak_fun()
            if obj is not None and fun is not None:
                return fun(obj, *things)
            elif self.handle is not None:
                self.sender.disconnect(self.handle)
                self.handle = None
                self.sender = None
    
    def weak_connect(sender, signal, callback):
        wrapper = CallbackWrapper(sender, callback)
        wrapper.handle = sender.connect(signal, wrapper)
        return wrapper
    

    (this is a proof of concept code, works for me — you should probably adapt this piece to your needs). Few notes:

    • I am storing callback object and function separatelly. You cannot simply make a weakref of a bound method, because bound methods are very temporary objects. Actually weakref.ref(obj.method) will destroy the bound method object instantly after creating a weakref. I didn’t check whether it is needed to store a weakref to the function too… I guess if your code is static, you probably can avoid that.
    • The object wrapper will remove itself from the signal sender when it notices that the weak reference ceased to exist. This is also necessary to destroy the circular dependence between the CallbackWrapper and the signal sender object.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class A that I overload its operator=. However it is required
I wrote a python script that rotates an image 90 degrees. I am including
I have a confusing little problem in SQL-Server 2005 and Classic ASP. I have
I have a table that has notes in it. It has a bunch of
I'd like to download, extract and iterate over a text file in Python without
This is a bit of a WPF whodunnit! My validation temples it not appearing
here is my problem. I'm using MVC and in a lot of Index Views
This may be impossible, but I thought I'd ask before I rework the whole
I'm mirroring some internal websites for backup purposes. As of right now I basically
I'm currently writing a PyGTK application and I'd like some advice as to the

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.