fix(config): Free previous string value before setting new one
- Added freeString checks before dupeString in setFieldByDef - Prevents memory leak during hot-reload when same field is set multiple times - Works with Config structs that implement freeString method
This commit is contained in:
parent
19c9f34b55
commit
3d30b1ed24
1 changed files with 14 additions and 0 deletions
|
|
@ -390,6 +390,13 @@ pub fn Engine(comptime variables: []const ConfigVariable, comptime ConfigType: t
|
|||
// IMPORTANTE: Duplicar el string porque 'value' apunta a 'content'
|
||||
// que se libera con defer al final de load()
|
||||
if (@hasDecl(ConfigType, "dupeString")) {
|
||||
// Liberar string anterior si existe (evitar leak en hot-reload)
|
||||
if (@hasDecl(ConfigType, "freeString")) {
|
||||
const old_val = @field(config.*, v.name);
|
||||
if (old_val.len > 0) {
|
||||
_ = config.freeString(old_val);
|
||||
}
|
||||
}
|
||||
// Config tiene método para trackear strings alocados
|
||||
@field(config.*, v.name) = config.dupeString(value) catch value;
|
||||
} else if (@hasField(ConfigType, "allocator")) {
|
||||
|
|
@ -409,6 +416,13 @@ pub fn Engine(comptime variables: []const ConfigVariable, comptime ConfigType: t
|
|||
// String que representa un color (nombre de paleta)
|
||||
// También necesita duplicarse
|
||||
if (@hasDecl(ConfigType, "dupeString")) {
|
||||
// Liberar string anterior si existe (evitar leak en hot-reload)
|
||||
if (@hasDecl(ConfigType, "freeString")) {
|
||||
const old_val = @field(config.*, v.name);
|
||||
if (old_val.len > 0) {
|
||||
_ = config.freeString(old_val);
|
||||
}
|
||||
}
|
||||
@field(config.*, v.name) = config.dupeString(value) catch value;
|
||||
} else if (@hasField(ConfigType, "allocator")) {
|
||||
@field(config.*, v.name) = config.allocator.dupe(u8, value) catch value;
|
||||
|
|
|
|||
Loading…
Reference in a new issue