-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Segmentation fault in ZMessage #8
Comments
Thanks for reporting the issue! Please give me some time to reproduce the issue and have a look at possible solutions. Also thanks for proposing one solution here. |
I believe I'm having the same, or similar, issue. It's very possible I'm using it wrong, if so, please advise. const std = @import("std");
const zzmq = @import("zzmq");
fn free_with_defer(socket: *zzmq.ZSocket, allocator: std.mem.Allocator) !void {
const buffer = try allocator.alloc(u8, 16);
defer allocator.free(buffer);
var message = try zzmq.ZMessage.initUnmanaged(buffer, null);
defer message.deinit();
try socket.send(&message, .{});
}
fn provide_allocator(socket: *zzmq.ZSocket, allocator: std.mem.Allocator) !void {
const buffer = try allocator.alloc(u8, 16);
var message = try zzmq.ZMessage.initUnmanaged(buffer, allocator);
defer message.deinit();
try socket.send(&message, .{});
}
test "minimal" {
var context = try zzmq.ZContext.init(std.testing.allocator);
defer context.deinit();
var pusher = try zzmq.ZSocket.init(zzmq.ZSocketType.Push, &context);
defer pusher.deinit();
try pusher.connect("inproc://1");
// this causes segfault
// try free_with_defer(pusher, std.testing.allocator);
// this causes memory leak
try provide_allocator(pusher, std.testing.allocator);
var puller = try zzmq.ZSocket.init(zzmq.ZSocketType.Pull, &context);
defer puller.deinit();
try puller.bind("inproc://1");
var payload_frame = try puller.receive(.{});
defer payload_frame.deinit();
const payload = try payload_frame.data();
std.debug.print("payload: {s}", .{payload});
}
The provided solution in the first post didn't fix it in my case. Oh, and the same issue happens whether the |
Actually I had applied the patch wrong. The proposed solution works, when both freeing with defer and proving the allocator in const std = @import("std");
const zzmq = @import("zzmq");
fn free_with_defer_and_provide_allocator(socket: *zzmq.ZSocket, allocator: std.mem.Allocator) !void {
const buffer = try allocator.alloc(u8, 16);
defer allocator.free(buffer);
var message = try zzmq.ZMessage.init(allocator, buffer);
defer message.deinit();
try socket.send(&message, .{});
}
test "minimal" {
var context = try zzmq.ZContext.init(std.testing.allocator);
defer context.deinit();
var pusher = try zzmq.ZSocket.init(zzmq.ZSocketType.Push, &context);
defer pusher.deinit();
try pusher.connect("inproc://1");
try free_with_defer_and_provide_allocator(pusher, std.testing.allocator);
var puller = try zzmq.ZSocket.init(zzmq.ZSocketType.Pull, &context);
defer puller.deinit();
try puller.bind("inproc://1");
var payload_frame = try puller.receive(.{});
defer payload_frame.deinit();
const payload = try payload_frame.data();
std.debug.print("payload: {any}", .{payload});
} |
For sending, if
ZMessage
is created in stack frame, segmentation fault was reported.Following code is example.
If
ZExternalMessage.refRelease
is called after exit function, it will be released dangling pointer.Resolving this issue, it must allocate ZInternalMessage into heap.
For instance: in ZMessage.init method
Since this
ZMessageInternal
variant ofZMessage
goes on keeping in heap memory, It can release safety.In addition to this solution, as
ZMessage.initUnmanaged
does not always passallocator
, it may be needInternalUnmanaged
variant of ZMessageImpl.The text was updated successfully, but these errors were encountered: