Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 14, 2024
1 parent 5deb1cb commit 5d6b849
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
24 changes: 13 additions & 11 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -60274,6 +60274,7 @@ int ecs_script_visit_(

#ifdef FLECS_SCRIPT

static
int flecs_script_check_expr(
ecs_script_eval_visitor_t *v,
ecs_expr_node_t **expr_ptr,
Expand Down Expand Up @@ -61930,8 +61931,10 @@ int flecs_script_eval_for_range(
ecs_script_eval_visitor_t *v,
ecs_script_for_range_t *node)
{
ecs_value_t from_val = { .type = ecs_id(ecs_i32_t) };
ecs_value_t to_val = { .type = ecs_id(ecs_i32_t) };
int32_t from;
int32_t to;
ecs_value_t from_val = { .type = ecs_id(ecs_i32_t), .ptr = &from };
ecs_value_t to_val = { .type = ecs_id(ecs_i32_t), .ptr = &to };

if (flecs_script_eval_expr(v, &node->from, &from_val)) {
return -1;
Expand All @@ -61941,9 +61944,6 @@ int flecs_script_eval_for_range(
return -1;
}

int32_t from = *(int32_t*)from_val.ptr;
int32_t to = *(int32_t*)to_val.ptr;

v->vars = flecs_script_vars_push(v->vars, &v->r->stack, &v->r->allocator);

ecs_script_var_t *var = ecs_script_vars_declare(v->vars, node->loop_var);
Expand Down Expand Up @@ -78847,10 +78847,10 @@ int flecs_expr_interpolated_string_visit_type(
}

/* Fiddly, but reduces need for allocations */
ecs_size_t offset = flecs_ito(
int32_t, node->buffer - node->value);
var_name = ECS_OFFSET(var_name, offset);
(*(char*)ECS_OFFSET(ptr, offset)) = '\0';
ecs_size_t var_name_pos = flecs_ito(int32_t, var_name - node->value);
var_name = &node->buffer[var_name_pos];
ecs_size_t var_name_end = flecs_ito(int32_t, ptr - node->value);
node->buffer[var_name_end] = '\0';

ecs_expr_variable_t *var = flecs_expr_variable_from(
script, (ecs_expr_node_t*)node, var_name);
Expand Down Expand Up @@ -78898,6 +78898,7 @@ int flecs_expr_interpolated_string_visit_type(
result = (ecs_expr_node_t*)flecs_expr_cast(script,
(ecs_expr_node_t*)result, ecs_id(ecs_string_t));
if (!result) {
/* Cast failed */
goto error;
}
}
Expand Down Expand Up @@ -79015,11 +79016,12 @@ int flecs_expr_initializer_visit_type(
}

if (elem->value->type != elem_type) {
elem->value = (ecs_expr_node_t*)flecs_expr_cast(
ecs_expr_node_t *cast = (ecs_expr_node_t*)flecs_expr_cast(
script, elem->value, elem_type);
if (!elem->value) {
if (!cast) {
goto error;
}
elem->value = cast;
}

if (!is_opaque) {
Expand Down
2 changes: 1 addition & 1 deletion distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
* When enabled, Flecs will use the OS allocator provided in the OS API directly
* instead of the builtin block allocator. This can decrease memory utilization
* as memory will be freed more often, at the cost of decreased performance. */
// #define FLECS_USE_OS_ALLOC
#define FLECS_USE_OS_ALLOC

/** @def FLECS_ID_DESC_MAX
* Maximum number of ids to add ecs_entity_desc_t / ecs_bulk_desc_t */
Expand Down
2 changes: 1 addition & 1 deletion include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@
* When enabled, Flecs will use the OS allocator provided in the OS API directly
* instead of the builtin block allocator. This can decrease memory utilization
* as memory will be freed more often, at the cost of decreased performance. */
// #define FLECS_USE_OS_ALLOC
#define FLECS_USE_OS_ALLOC

/** @def FLECS_ID_DESC_MAX
* Maximum number of ids to add ecs_entity_desc_t / ecs_bulk_desc_t */
Expand Down
14 changes: 8 additions & 6 deletions src/addons/script/expr/visit_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,10 @@ int flecs_expr_interpolated_string_visit_type(
}

/* Fiddly, but reduces need for allocations */
ecs_size_t offset = flecs_ito(
int32_t, node->buffer - node->value);
var_name = ECS_OFFSET(var_name, offset);
(*(char*)ECS_OFFSET(ptr, offset)) = '\0';
ecs_size_t var_name_pos = flecs_ito(int32_t, var_name - node->value);
var_name = &node->buffer[var_name_pos];
ecs_size_t var_name_end = flecs_ito(int32_t, ptr - node->value);
node->buffer[var_name_end] = '\0';

ecs_expr_variable_t *var = flecs_expr_variable_from(
script, (ecs_expr_node_t*)node, var_name);
Expand Down Expand Up @@ -617,6 +617,7 @@ int flecs_expr_interpolated_string_visit_type(
result = (ecs_expr_node_t*)flecs_expr_cast(script,
(ecs_expr_node_t*)result, ecs_id(ecs_string_t));
if (!result) {
/* Cast failed */
goto error;
}
}
Expand Down Expand Up @@ -734,11 +735,12 @@ int flecs_expr_initializer_visit_type(
}

if (elem->value->type != elem_type) {
elem->value = (ecs_expr_node_t*)flecs_expr_cast(
ecs_expr_node_t *cast = (ecs_expr_node_t*)flecs_expr_cast(
script, elem->value, elem_type);
if (!elem->value) {
if (!cast) {
goto error;
}
elem->value = cast;
}

if (!is_opaque) {
Expand Down
1 change: 1 addition & 0 deletions src/addons/script/visit_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifdef FLECS_SCRIPT
#include "script.h"

static
int flecs_script_check_expr(
ecs_script_eval_visitor_t *v,
ecs_expr_node_t **expr_ptr,
Expand Down
9 changes: 4 additions & 5 deletions src/addons/script/visit_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,10 @@ int flecs_script_eval_for_range(
ecs_script_eval_visitor_t *v,
ecs_script_for_range_t *node)
{
ecs_value_t from_val = { .type = ecs_id(ecs_i32_t) };
ecs_value_t to_val = { .type = ecs_id(ecs_i32_t) };
int32_t from;
int32_t to;
ecs_value_t from_val = { .type = ecs_id(ecs_i32_t), .ptr = &from };
ecs_value_t to_val = { .type = ecs_id(ecs_i32_t), .ptr = &to };

if (flecs_script_eval_expr(v, &node->from, &from_val)) {
return -1;
Expand All @@ -1177,9 +1179,6 @@ int flecs_script_eval_for_range(
return -1;
}

int32_t from = *(int32_t*)from_val.ptr;
int32_t to = *(int32_t*)to_val.ptr;

v->vars = flecs_script_vars_push(v->vars, &v->r->stack, &v->r->allocator);

ecs_script_var_t *var = ecs_script_vars_declare(v->vars, node->loop_var);
Expand Down
2 changes: 1 addition & 1 deletion test/script/src/ExprAst.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ void ExprAst_interpolated_string_curly_brackets_w_var(void) {
void ExprAst_template_w_foldable_const(void) {
ecs_world_t *world = ecs_init();

ecs_entity_t ecs_id(Position) = ecs_struct(world, {
ecs_struct(world, {
.entity = ecs_entity(world, {.name = "Position"}),
.members = {
{"x", ecs_id(ecs_f32_t)},
Expand Down

0 comments on commit 5d6b849

Please sign in to comment.