Fixing memory leaks.

mess
Karchnu 2020-12-22 18:11:47 +01:00
parent c460858a69
commit 5adcd203f9
1 changed files with 44 additions and 30 deletions

View File

@ -9,7 +9,7 @@ const ArrayList = std.ArrayList;
const PropertyHashMap = AutoHashMap([] const u8, PropertyValue);
const NodeList = ArrayList(*Node); // Store only references, or it won't be freeable.
const NodeList = ArrayList(Node);
const Definitions = AutoHashMap([] const u8, Node);
@ -26,6 +26,21 @@ fn print_node(node: Node) void {
}
}
fn print_tree(tree: Tree) void {
say("tree.definitions:\n");
var it = tree.definitions.iterator();
while(it.next()) |kv| {
std.debug.print("{} => ", .{kv.key});
const node = tree.definitions.get(kv.key);
if(node) |n| { print_node(n); }
}
say("tree.children:\n");
for(tree.children.items) |v, k| {
std.debug.print("{} => ", .{k});
print_node(v);
}
}
pub const Node = struct {
@ -38,7 +53,7 @@ pub const Node = struct {
pub fn deinit(self: *Node) void {
self.properties.deinit();
for (self.children.items) |value, index| {
for (self.children.items) |*value| {
value.deinit();
}
self.children.deinit();
@ -78,7 +93,7 @@ pub const Tree = struct {
pub fn deinit(self: *Tree) void {
self.definitions.deinit();
for (self.children.items) |value, index| {
for (self.children.items) |*value| {
value.deinit();
}
self.children.deinit();
@ -110,6 +125,27 @@ test "simple test about structures" {
}
}
fn init_stuff(allocator: *Allocator) !Tree {
var tree = try Tree.create(allocator);
say("creating a definition and a few children\n");
try tree.definitions.put("MyObject", try Node.create(allocator, "my-type-name", "my-id"));
var new_node = try Node.create(allocator, "Object", "some-id-for-this-object");
var value = PropertyValue { .integer = 10 };
try new_node.properties.put("integer-val", value);
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"));
print_tree(tree);
return tree;
}
test "init a Tree structure" {
// const allocator = std.heap.page_allocator;
// Allocator with safety on: checking for memory leaks.
@ -119,11 +155,14 @@ test "init a Tree structure" {
say("\n");
say("creating a tree\n");
var tree = try Tree.create(allocator);
var tree = try init_stuff(allocator);
// We want to know if there are memory leaks.
defer {
tree.deinit();
// tree.deinit() catch |err| switch(err) {
// else => {say("an error occured\n");}
// } ;
say("\n\ntesting for memory leaks\n");
const leaks = gpa.deinit();
@ -135,31 +174,6 @@ test "init a Tree structure" {
}
}
say("creating a definition and a few children\n");
try tree.definitions.put("MyObject", try Node.create(allocator, "my-type-name", "my-id"));
var new_node = try Node.create(allocator, "Object", "some-id-for-this-object");
var value = PropertyValue { .integer = 10 };
try new_node.properties.put("integer-val", value);
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")));
say("tree.definitions:\n");
var it = tree.definitions.iterator();
while(it.next()) |kv| {
std.debug.print("{} => ", .{kv.key});
const node = tree.definitions.get(kv.key);
if(node) |n| { print_node(n); }
}
say("tree.children:\n");
for(tree.children.items) |v, k| {
std.debug.print("{} => ", .{k});
print_node(v.*);
}
print_tree(tree);
}