Permalink
Browse files

ugly GC workaround, but it allows long-lived callbacks :) #LD48

  • Loading branch information...
1 parent faeae51 commit b8400606077a6e1ce09ccb1fddf46fac50448f18 Amos Wenger committed Dec 17, 2011
Showing with 13 additions and 10 deletions.
  1. +13 −10 source/gtk/_GObject.ooc
View
@@ -1,18 +1,26 @@
use gtk
import gtk/Gtk
+import structs/ArrayList
+
+// FIXME: right now we have no way to remove event handlers
+// to clean up
+
+// retain references to context struct to avoid them to be collected
+gcHack := ArrayList<Pointer> new()
+
_GObjectStruct: cover from GtkObject
/**
* The base of the GObject hierarchy. Can send and receive signals
*/
_GObject: cover from _GObjectStruct* {
- connect: func ~nodata (signalName: String, callback: Func (...)) -> GULong {
- closure: Closure* = gc_malloc(Closure size)
- closure@ thunk = callback as Closure thunk
- closure@ context = callback as Closure context
- g_signal_connect_swapped(this, signalName, GTK_SIGNAL_FUNC(thunk), closure)
+ connect: func ~nodata (signalName: String, callback: Func) -> GULong {
+ c: Closure = callback as Closure
+ // FIXME: this will leak, we haven't added a way to remove callbacks
+ gcHack add(c context)
+ g_signal_connect_swapped(this, signalName, GTK_SIGNAL_FUNC(c thunk), c context)
}
connectNaked: func (signalName: String, context, callback: Pointer) -> GULong {
@@ -24,11 +32,6 @@ _GObject: cover from _GObjectStruct* {
}
-thunk: func (userData: Closure*, object: _GObject, event: Pointer) -> Bool {
- realFunc := userData@ as Func ()
- realFunc()
-}
-
nakedThunk: func (userData: Closure*, object: _GObject, event: Pointer) -> Bool {
realFunc := userData@ as Func (Pointer)
data := realFunc as Closure context

0 comments on commit b840060

Please sign in to comment.