#Does order of errdefer matter?

1 messages · Page 1 of 1 (latest)

late edge
#

Does the ordering of errdefer matter when cleaning up memory? For example, technically the struct variable should be freed before the struct that holds it. i.e. Does the order matter here? or can we assume that the memory is ok until we loose scope?

pub fn create(font: *Font, text: []const u8, box: Box, color: ray.Color, update_helper: TextUpdateFunc, allocator: std.mem.Allocator) !*TextElement {
    var element = try allocator.create(TextElement);
    errdefer allocator.destroy(element);
    element.text = try allocator.dupeZ(u8, text);
    errdefer allocator.free(element.text);
    ....
    error happens
paper copper
late edge
#

Yes thats a typo in my example. I have updated it for the example. So in the example, does the order matter? And is errdefer called in the right order if an error occurs later?

#

I assume that element.text would normally need to be freed, then element itself. I don`t know if that is what would happen here and if it matters.

noble fulcrum
#

the order is same as defers

late edge