From 353986763c34a962245dc9d6b2f3befa16930b8a Mon Sep 17 00:00:00 2001 From: Ritalin Date: Sat, 30 Dec 2023 22:43:35 +0900 Subject: [PATCH 1/3] Supports retain/release for Object. --- src/object.zig | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/object.zig b/src/object.zig index 339e08b..833689e 100644 --- a/src/object.zig +++ b/src/object.zig @@ -96,8 +96,19 @@ pub const Object = struct { const ivar = c.object_getInstanceVariable(self.value, name, null); c.object_setIvar(self.value, ivar, val.value); } + + pub fn retain(self: Object) Object { + return fromId(objc_retain(self.value)); + } + + pub fn release(self: Object) void { + objc_release(self.value); + } }; +extern "c" fn objc_retain(objc.c.id) objc.c.id; +extern "c" fn objc_release(objc.c.id) void; + test { const testing = std.testing; const NSObject = objc.getClass("NSObject").?; @@ -108,3 +119,24 @@ test { try testing.expectEqualStrings("NSObject", obj.getClassName()); obj.msgSend(void, objc.sel("dealloc"), .{}); } + +fn retainCount(obj: Object) c_ulong { + return obj.msgSend(c_ulong, objc.Sel.registerName("retainCount"), .{}); +} + +test "retain object" { + const testing = std.testing; + const NSObject = objc.getClass("NSObject").?; + + const obj = NSObject.msgSend(objc.Object, objc.Sel.registerName("alloc"), .{}); + _ = obj.msgSend(objc.Object, objc.Sel.registerName("init"), .{}); + try testing.expectEqual(@as(c_ulong, 1), retainCount(obj)); + + _ = obj.retain(); + try testing.expectEqual(@as(c_ulong, 2), retainCount(obj)); + + obj.release(); + try testing.expectEqual(@as(c_ulong, 1), retainCount(obj)); + + obj.msgSend(void, objc.sel("dealloc"), .{}); +} \ No newline at end of file From 74dc09dda8a89d0c9ced3786ec6c7b23936d1eaa Mon Sep 17 00:00:00 2001 From: Ritalin Date: Sat, 30 Dec 2023 23:00:49 +0900 Subject: [PATCH 2/3] Add method comment --- src/object.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/object.zig b/src/object.zig index 833689e..0700a49 100644 --- a/src/object.zig +++ b/src/object.zig @@ -97,10 +97,15 @@ pub const Object = struct { c.object_setIvar(self.value, ivar, val.value); } + /// In MacOS SDK, the memory is managed by ARC(Automatic Reference Counting). + /// Therefore, it not must retain an object explictlly. + /// But if you'd like to keep reference of objc object in ziglang-side, it could use this method to avoid releqsing object by ARC. pub fn retain(self: Object) Object { return fromId(objc_retain(self.value)); } + /// if you have use the retain method, you must call the release method. + /// Otherwise, a memory leak will occur. pub fn release(self: Object) void { objc_release(self.value); } From 56dc708c7ad6f9c19de34825a5771a3ef1bc5389 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Jan 2024 20:43:01 -0800 Subject: [PATCH 3/3] style changes --- src/object.zig | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/object.zig b/src/object.zig index 0700a49..ff2f170 100644 --- a/src/object.zig +++ b/src/object.zig @@ -97,15 +97,10 @@ pub const Object = struct { c.object_setIvar(self.value, ivar, val.value); } - /// In MacOS SDK, the memory is managed by ARC(Automatic Reference Counting). - /// Therefore, it not must retain an object explictlly. - /// But if you'd like to keep reference of objc object in ziglang-side, it could use this method to avoid releqsing object by ARC. pub fn retain(self: Object) Object { return fromId(objc_retain(self.value)); } - /// if you have use the retain method, you must call the release method. - /// Otherwise, a memory leak will occur. pub fn release(self: Object) void { objc_release(self.value); } @@ -114,6 +109,10 @@ pub const Object = struct { extern "c" fn objc_retain(objc.c.id) objc.c.id; extern "c" fn objc_release(objc.c.id) void; +fn retainCount(obj: Object) c_ulong { + return obj.msgSend(c_ulong, objc.Sel.registerName("retainCount"), .{}); +} + test { const testing = std.testing; const NSObject = objc.getClass("NSObject").?; @@ -125,10 +124,6 @@ test { obj.msgSend(void, objc.sel("dealloc"), .{}); } -fn retainCount(obj: Object) c_ulong { - return obj.msgSend(c_ulong, objc.Sel.registerName("retainCount"), .{}); -} - test "retain object" { const testing = std.testing; const NSObject = objc.getClass("NSObject").?; @@ -144,4 +139,4 @@ test "retain object" { try testing.expectEqual(@as(c_ulong, 1), retainCount(obj)); obj.msgSend(void, objc.sel("dealloc"), .{}); -} \ No newline at end of file +}