Memory leaks kinda fixed.

mess
Karchnu 2020-12-22 05:53:48 +01:00
parent 0e1dcf7342
commit c460858a69
1 changed files with 16 additions and 14 deletions

View File

@ -7,6 +7,13 @@ const HashMap = std.HashMap;
const AutoHashMap = std.AutoHashMap;
const ArrayList = std.ArrayList;
const PropertyHashMap = AutoHashMap([] const u8, PropertyValue);
const NodeList = ArrayList(*Node); // Store only references, or it won't be freeable.
const Definitions = AutoHashMap([] const u8, Node);
// Private functions.
fn say(tosay: []const u8) void {
std.debug.print("{}", .{tosay});
}
@ -20,10 +27,6 @@ fn print_node(node: Node) void {
}
const PropertyHashMap = AutoHashMap([] const u8, PropertyValue);
const NodeList = ArrayList(Node);
const Definitions = AutoHashMap([] const u8, Node);
pub const Node = struct {
id: ?[] const u8,
@ -35,8 +38,8 @@ pub const Node = struct {
pub fn deinit(self: *Node) void {
self.properties.deinit();
for (self.children.items) |k, v| {
v.deinit();
for (self.children.items) |value, index| {
value.deinit();
}
self.children.deinit();
}
@ -75,9 +78,8 @@ pub const Tree = struct {
pub fn deinit(self: *Tree) void {
self.definitions.deinit();
for (self.children.items) |k, v| {
std.debug.print("INSIDE DEINIT: {} => {}\n", .{k,v});
// v.deinit();
for (self.children.items) |value, index| {
value.deinit();
}
self.children.deinit();
}
@ -141,10 +143,10 @@ test "init a Tree structure" {
value = PropertyValue { .string = "some value" };
try new_node.properties.put("string-val", value);
try tree.children.append(new_node);
try tree.children.append(try Node.create(allocator, "Object", "my-id"));
try tree.children.append(try Node.create(allocator, "OtherObject", null));
try tree.children.append(try Node.create(allocator, "Text", "my-id-for-text-object"));
try tree.children.append(&new_node);
try tree.children.append(&(try Node.create(allocator, "Object", "my-id")));
try tree.children.append(&(try Node.create(allocator, "OtherObject", null)));
try tree.children.append(&(try Node.create(allocator, "Text", "my-id-for-text-object")));
say("tree.definitions:\n");
var it = tree.definitions.iterator();
@ -157,7 +159,7 @@ test "init a Tree structure" {
say("tree.children:\n");
for(tree.children.items) |v, k| {
std.debug.print("{} => ", .{k});
print_node(v);
print_node(v.*);
}
}