-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a memory safe way to connect signals.
Fixes #150
- Loading branch information
Showing
7 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require "./spec_helper" | ||
|
||
private class Obj < GObject::Object | ||
getter int32 = 0 | ||
getter bool = false | ||
|
||
signal int32_bool(int32 : Int32, bool : Bool) | ||
|
||
def initialize(int32_value : Int32, bool_value : Bool) | ||
super() | ||
# Uncomment this and the test will fail, because a reference is left on ClosureManager | ||
# | ||
# int32_bool_signal.connect do |int, bool| | ||
# int32_bool_slot(int, bool) | ||
# end | ||
|
||
GObject.connect(int32_bool_signal, int32_bool_slot(Int32, Bool)) | ||
int32_bool_signal.emit(int32_value, bool_value) | ||
end | ||
|
||
def int32_bool_slot(@int32, @bool) | ||
end | ||
end | ||
|
||
@[NoInline] | ||
private def create_obj(int32_value : Int32, bool_value : Bool) | ||
obj = Obj.new(int32_value, bool_value) | ||
{WeakRef.new(obj), obj.int32, obj.bool} | ||
end | ||
|
||
private class Subject < Test::Subject | ||
getter enum : Test::RegularEnum = Test::RegularEnum::Value1 | ||
|
||
def initialize(enum_value : Test::RegularEnum) | ||
super() | ||
GObject.connect(enum_signal, enum_slot(Test::RegularEnum)) | ||
enum_signal.emit(enum_value) | ||
end | ||
|
||
def enum_slot(@enum) | ||
end | ||
end | ||
|
||
@[NoInline] | ||
private def create_subject(enum_value) | ||
obj = Subject.new(enum_value) | ||
{WeakRef.new(obj), obj.enum} | ||
end | ||
|
||
describe "Save signals" do | ||
it "does not leak memory" do | ||
obj_ref, int32, bool = create_obj(int32_value: 42_u32, bool_value: true) | ||
int32.should eq(42) | ||
bool.should eq(true) | ||
# Need to call some obj_ref method to avoid LLVM optimizations that break this test. | ||
obj_ref.inspect | ||
GC.collect | ||
obj_ref.value.should eq(nil) | ||
end | ||
|
||
it "works for non-user signals the same way" do | ||
obj_ref, enum_ = create_subject(enum_value: Test::RegularEnum::Value2) | ||
enum_.should eq(Test::RegularEnum::Value2) | ||
obj_ref.inspect | ||
GC.collect | ||
obj_ref.value.should eq(nil) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require "weak_ref" | ||
|
||
module GObject | ||
@[Experimental("Join discussion about this topic at https://github.com/hugopl/gi-crystal/issues/150")] | ||
macro connect(signal, handler) | ||
begin | ||
%box = ::Box.box(WeakRef.new(self)) | ||
%sig = {{ signal }} | ||
%sig.validate_params({{ handler.args.splat }}) | ||
|
||
# Declare "C" slot | ||
%c_slot = ->(sender : Void*, | ||
{% i = 0 %} | ||
{% for arg in handler.args %} | ||
{% | ||
resolved_type = arg.resolve | ||
if resolved_type == String || resolved_type == Path | ||
type = ::Pointer(UInt8) | ||
elsif resolved_type == Bool | ||
type = ::Int32 | ||
else | ||
type = resolved_type | ||
end | ||
%} | ||
c_arg_{{ i }} : {{ type }}, | ||
{% i += 1 %} | ||
{% end %} | ||
box : Void*) { | ||
|
||
ref = ::Box(WeakRef(typeof(self))).unbox(box).value || raise GICrystal::ObjectCollectedError.new | ||
|
||
# Convert from C | ||
{% i = 0 %} | ||
{% for arg in handler.args %} | ||
{% resolved_type = arg.resolve %} | ||
{% if resolved_type == String %} | ||
arg_{{ i }} = String.new(c_arg_{{ i }}) | ||
{% elsif resolved_type == Path %} | ||
arg_{{ i }} = Path.new(String.new(c_arg_{{ i }}) | ||
{% elsif resolved_type == Bool %} | ||
arg_{{ i }} = GICrystal.to_bool(c_arg_{{ i }}) | ||
{% else %} | ||
arg_{{ i }} = c_arg_{{ i }} | ||
{% end %} | ||
{% i += 1 %} | ||
{% end %} | ||
|
||
ref.{{ handler.name }}( {{ (0...handler.args.size).map { |i| "arg_#{i}".id }.splat }} ) | ||
nil | ||
} | ||
sig_handler_id = LibGObject.g_signal_connect_data(%sig.source, %sig.name, %c_slot.pointer, | ||
GICrystal::ClosureDataManager.register(%box), | ||
->GICrystal::ClosureDataManager.deregister, 0_u32) | ||
|
||
GObject::SignalConnection.new(%sig.source, sig_handler_id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters