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

Ask A Question

Stats

  • Questions 197k
  • Answers 197k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See CGI::Application and CGI::Session with CGI::Application::Plugin::Session. May 12, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer This does come from MS Access. KB275085 explains that the… May 12, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer Have you tried: $("#elem1").autocomplete("source1.php").addClass("class1"); $("#elem2").autocomplete("source2.php").addClass("class2"); $("#elem3").autocomplete("source3.php").addClass("class3"); May 12, 2026 at 7:17 pm

Related Questions

The problem is basically this, in python's gobject and gtk bindings. Assume we have
Basically for this case, I am using the _winreg module in Python v2.6 but
I am writing an application in Pylons that relies on the output of some
TL;DR: I've supplied a patch for a bug I found and I've got 0
Is there an algorithm to estimate the median, mode, skewness, and/or kurtosis of set

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.