Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Connect closure memory leaks #55

Open
jrick opened this issue Apr 24, 2014 · 0 comments
Open

Connect closure memory leaks #55

jrick opened this issue Apr 24, 2014 · 0 comments

Comments

@jrick
Copy link
Member

jrick commented Apr 24, 2014

It is currently possible to create (*glib.Object).Connect closures which close over gotk3 objects, or include them as part of the userdata. This results in the glib package creating a closure context with (reflected) references to the object and saving it globally in a map of GClosures to closure contexts.

Closure contexts are never removed from glib's global map until the GLib runtime frees (when ref count drops to 0) the object the closure is connected to and finalizes the closure. However, these closure contexts still hold references to gotk3 objects, and because they are still technically accessable through the glib closure context map, the Go garbage collecter is unable to know that the objects are no longer live. This results in the gotk3 object, the Go closure, the closure context, and the GClosure never being freed.

The following code snippet demonstrates the problem:

{
    b, err := gtk.ButtonNew()
    if err != nil {
        // badness
    }
    runtime.SetFinalizer(b, func(_ *gtk.Button) {
        fmt.Println("finalizing button")
    })
    b.Connect("clicked", func() {
        _ = b // This introduces the circular reference
    })
} // b is out of scope, and was never added to another container
runtime.GC() // b's finalizer does not run
jrick added a commit to jrick/xombrero2 that referenced this issue Apr 25, 2014
This change manually removes the connected signal by their id for
gotk3 and go-webkit2 objects for HTML pages when a tab is closed.
This prevents a leak where connected signals can prevent finalizers
from running (see conformal/gotk3#55).

Also, rather than waiting for the glib.Object finalizer on each
wk2.WebView (which may take up to 3 minutes before the Go GC runs),
explicitly .Destroy() the webview widget.  This causes the
WebKitWebProcess to be immediately closed.
@jrick jrick changed the title Closure memory leaks Connect closure memory leaks Jun 23, 2014
andre-hub added a commit to andre-hub/gotk3 that referenced this issue Feb 13, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant