feat(core): fix segfault

This commit is contained in:
2025-04-23 17:41:51 +02:00
parent 490425bfd9
commit 8a39c36aa8
3 changed files with 71 additions and 50 deletions

View File

@@ -14,13 +14,8 @@ pub const Item = struct {
parent: ?AbsolutePath, parent: ?AbsolutePath,
item: ItemEnum, item: ItemEnum,
errors: std.ArrayList(Error), errors: std.ArrayList(Error),
deiniting: bool = false,
deinited: bool = false,
pub fn deinit(self: *Item) void { pub fn deinit(self: *Item) void {
if (self.deinited) return;
self.deiniting = true;
self.allocator.free(self.name); self.allocator.free(self.name);
self.allocator.free(self.displayName); self.allocator.free(self.displayName);
self.allocator.free(self.fullName.path); self.allocator.free(self.fullName.path);
@@ -31,7 +26,6 @@ pub const Item = struct {
self.allocator.free(e.content); self.allocator.free(e.content);
} }
self.item.deinit(); self.item.deinit();
self.deinited = true;
} }
}; };

View File

@@ -35,6 +35,7 @@ fn loadChildren(container: *Container, deinitTracker: *DeinitTracker) void {
var it = dir.iterate(); var it = dir.iterate();
while (it.next() catch return) |entry| { while (it.next() catch return) |entry| {
const child = container.item.fullName.getChild(entry.name, container.item.allocator) catch return; const child = container.item.fullName.getChild(entry.name, container.item.allocator) catch return;
if (deinitTracker.deinited) return; if (deinitTracker.deinited) return;
container.children.append(child) catch return; container.children.append(child) catch return;
} }
@@ -64,6 +65,67 @@ pub const LocalContentProvider = struct {
globalAllocator: std.mem.Allocator, globalAllocator: std.mem.Allocator,
) GetItemsError!Item { ) GetItemsError!Item {
const stat = std.fs.cwd().statFile(fullName.path) catch return GetItemsError.NotExists; const stat = std.fs.cwd().statFile(fullName.path) catch return GetItemsError.NotExists;
return switch (stat.kind) {
.directory => blk: {
const container = try allocator.create(Container);
container.* = Container{
.children = std.ArrayList(FullName).init(allocator),
.childrenLoading = true,
.item = undefined,
};
const val: ItemEnum = .{
.container = container,
};
try initItem(
self,
&container.item,
val,
fullName,
allocator,
globalAllocator,
);
try self.threadPool.spawn(loadChildren, .{ container, container.item.deinitTracker });
break :blk container.item;
},
.file => blk: {
const element = try allocator.create(Element);
element.* = Element{
.item = undefined,
};
const val: ItemEnum = .{
.element = element,
};
try initItem(
self,
&element.item,
val,
fullName,
allocator,
globalAllocator,
);
break :blk element.item;
},
else => @panic(
"Unsupported file type\n",
),
};
}
fn initItem(
contentProvider: *LocalContentProvider,
item: *Item,
innerItem: ItemEnum,
fullName: FullName,
allocator: std.mem.Allocator,
globalAllocator: std.mem.Allocator,
) !void {
var deinitTracker = try globalAllocator.create(DeinitTracker); var deinitTracker = try globalAllocator.create(DeinitTracker);
deinitTracker.init(globalAllocator); deinitTracker.init(globalAllocator);
@@ -81,55 +143,18 @@ pub const LocalContentProvider = struct {
const nativePath = try allocator.alloc(u8, fullName.path.len); const nativePath = try allocator.alloc(u8, fullName.path.len);
@memcpy(nativePath, fullName.path); @memcpy(nativePath, fullName.path);
var item = Item{ item.* = Item{
.allocator = allocator, .allocator = allocator,
.provider = self.provider(), .provider = contentProvider.provider(),
.deinitTracker = deinitTracker, .deinitTracker = deinitTracker,
.name = name, .name = name,
.displayName = displayName, .displayName = displayName,
.fullName = .{ .path = fullName2 }, .fullName = .{ .path = fullName2 },
.nativePath = models.NativePath{ .path = nativePath }, .nativePath = models.NativePath{ .path = nativePath },
.parent = null, .parent = null,
.item = undefined, .item = innerItem,
.errors = std.ArrayList(models.Error).init(allocator), .errors = std.ArrayList(models.Error).init(allocator),
}; };
const innerItem: ItemEnum = switch (stat.kind) {
.directory => blk: {
const container = try allocator.create(Container);
container.* = Container{
.children = std.ArrayList(FullName).init(allocator),
.childrenLoading = true,
.item = item,
};
const val: ItemEnum = .{
.container = container,
};
try self.threadPool.spawn(loadChildren, .{ container, deinitTracker });
break :blk val;
},
.file => blk: {
const element = try allocator.create(Element);
element.* = Element{
.item = item,
};
const val: ItemEnum = .{
.element = element,
};
break :blk val;
},
else => @panic(
"Unsupported file type\n",
),
};
item.item = innerItem;
return item;
} }
pub fn provider(self: *LocalContentProvider) Provider { pub fn provider(self: *LocalContentProvider) Provider {

View File

@@ -17,11 +17,12 @@ pub const Tab = struct {
currentItemsAllocator: ?std.heap.ArenaAllocator, currentItemsAllocator: ?std.heap.ArenaAllocator,
}; };
pub fn create( pub fn init(
self: *Tab,
threadPool: *std.Thread.Pool, threadPool: *std.Thread.Pool,
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
) Tab { ) void {
return Tab{ self.* = Tab{
.allocator = allocator, .allocator = allocator,
.currentItems = null, .currentItems = null,
.currentLocation = null, .currentLocation = null,
@@ -56,10 +57,11 @@ pub const Tab = struct {
pub fn deinit(self: *Tab) void { pub fn deinit(self: *Tab) void {
if (self.currentLocation) |c| { if (self.currentLocation) |c| {
c.deinit(); c.item.deinit();
} }
if (self._private.currentItemsAllocator) |arena| { if (self._private.currentItemsAllocator) |arena| {
arena.deinit(); arena.deinit();
} }
self.allocator.destroy(self);
} }
}; };