Working on memory leaks.

mess
Karchnu 2020-12-22 04:51:41 +01:00
parent 80a94a5fdd
commit 0e1dcf7342
1 changed files with 49 additions and 26 deletions

View File

@ -50,8 +50,6 @@ pub const Node = struct {
.gpa = allocator,
};
}
};
const PropertyValueTags = enum {
@ -69,27 +67,29 @@ pub const PropertyValue = union(PropertyValueTags) {
reference: [] const u8, // Reference to another property (property binding).
};
pub const Root = struct {
pub const Tree = struct {
definitions: Definitions,
children: NodeList,
gpa: *Allocator,
pub fn create(allocator: *Allocator) !Root {
return Root{
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();
}
self.children.deinit();
}
pub fn create(allocator: *Allocator) !Tree {
return Tree{
.definitions = Definitions.init(allocator),
.children = NodeList.init(allocator),
.gpa = allocator,
};
}
pub fn deinit(self: *Root) void {
self.definitions.deinit();
// for (self.children.items) |k, v| {
// k.deinit();
// }
self.children.deinit();
}
};
test "simple test about structures" {
@ -108,31 +108,54 @@ test "simple test about structures" {
}
}
test "init a Root structure" {
const allocator = std.heap.page_allocator;
test "init a Tree structure" {
// const allocator = std.heap.page_allocator;
// Allocator with safety on: checking for memory leaks.
var gpa = std.heap.GeneralPurposeAllocator(.{.safety = true}){};
const allocator = &gpa.allocator;
say("\n");
say("creating a root\n");
var root = try Root.create(allocator);
defer root.deinit();
say("creating a tree\n");
var tree = try Tree.create(allocator);
// We want to know if there are memory leaks.
defer {
tree.deinit();
say("\n\ntesting for memory leaks\n");
const leaks = gpa.deinit();
if (leaks) {
say("there were leaks, oh no\n");
}
else {
say("there were no leaks, youpi!\n");
}
}
say("creating a definition and a few children\n");
try root.definitions.put("MyObject", try Node.create(allocator, "my-type-name", "my-id"));
try root.children.append(try Node.create(allocator, "object", "my-id"));
try root.children.append(try Node.create(allocator, "otherObject", null));
try root.children.append(try Node.create(allocator, "Text", "my-id-for-text-object"));
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);
say("root.definitions:\n");
var it = root.definitions.iterator();
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 = root.definitions.get(kv.key);
const node = tree.definitions.get(kv.key);
if(node) |n| { print_node(n); }
}
say("root.children:\n");
for(root.children.items) |v, k| {
say("tree.children:\n");
for(tree.children.items) |v, k| {
std.debug.print("{} => ", .{k});
print_node(v);
}