Skip to content

Instantly share code, notes, and snippets.

@Numeez
Created October 15, 2025 05:08
Show Gist options
  • Select an option

  • Save Numeez/606b75bfc5a1c6d9a79e20950f3354a5 to your computer and use it in GitHub Desktop.

Select an option

Save Numeez/606b75bfc5a1c6d9a79e20950f3354a5 to your computer and use it in GitHub Desktop.
fn decCountInc(self: ?*SnekObject, allocator: std.mem.Allocator) void {
if (self == null) {
return;
}
self.?.referenceCount -= 1;
if (self.?.referenceCount == 0) {
SnekObject.refCountFree(self.?, allocator);
}
}
test "testing decrement ref count" {
const allocator = std.testing.allocator;
const x = SnekObject.newSnekInteger(allocator, 1);
try expect(x.?.referenceCount == 1);
SnekObject.refCountInc(x);
SnekObject.decCountInc(x, allocator);
try expect(x.?.referenceCount == 1);
SnekObject.decCountInc(x, allocator);
// If I call this multiple times after this like this
SnekObject.decCountInc(x, allocator);
SnekObject.decCountInc(x, allocator);
// This will result in Segfault
}
@Numeez
Copy link
Author

Numeez commented Oct 21, 2025

Thank you @Zorgatone
This is really helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment