solstice-solver

Solver library of the solstice app
git clone git://git.meso-star.com/solstice-solver.git
Log | Files | Refs | README | LICENSE

commit 39419653b80110af010a33e50455cbce041527cc
parent 1552e43034d631aa255b519b7e10bfa03a549e60
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 18 Jul 2016 14:59:27 +0200

Fix the ssol_object_instance_set_receiver function

A memory leak occurred when the function was invoked several times.

A NULL name is now authorized, meaning that the object instance is no
more a receiver.

Diffstat:
Msrc/ssol.h | 2+-
Msrc/ssol_object_instance.c | 17+++++++++--------
Msrc/ssol_object_instance_c.h | 5+++--
Msrc/test_ssol_object_instance.c | 4+++-
4 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/ssol.h b/src/ssol.h @@ -416,7 +416,7 @@ ssol_object_instance_set_transform SSOL_API res_T ssol_object_instance_set_receiver (struct ssol_object_instance* instance, - const char* name); + const char* name); /* May be NULL <=> it is no more a receiver */ SSOL_API res_T ssol_object_instance_is_attached diff --git a/src/ssol_object_instance.c b/src/ssol_object_instance.c @@ -39,7 +39,7 @@ object_instance_release(ref_T* ref) ASSERT(dev && dev->allocator); SSOL(object_ref_put(instance->object)); if(instance->shape) S3D(shape_ref_put(instance->shape)); - MEM_RM(dev->allocator, instance->receiver_name); + str_release(&instance->receiver_name); MEM_RM(dev->allocator, instance); SSOL(device_ref_put(dev)); } @@ -74,6 +74,7 @@ ssol_object_instantiate list_init(&instance->scene_attachment); instance->dev = dev; instance->object = object; + str_init(dev->allocator, &instance->receiver_name); SSOL(object_ref_get(object)); SSOL(device_ref_get(dev)); ref_init(&instance->ref); @@ -131,15 +132,15 @@ ssol_object_instance_set_receiver (struct ssol_object_instance* instance, const char* name) { - if (!instance || !name) + if(!instance) return RES_BAD_ARG; - /* keep name */ - instance->receiver_name = MEM_ALLOC(instance->dev->allocator, strlen(name) + 1); - if (!instance->receiver_name) return RES_MEM_ERR; - strcpy(instance->receiver_name, name); - - return RES_OK; + if(name) { + return str_set(&instance->receiver_name, name); + } else { + str_clear(&instance->receiver_name); + return RES_OK; + } } res_T diff --git a/src/ssol_object_instance_c.h b/src/ssol_object_instance_c.h @@ -16,8 +16,9 @@ #ifndef SSOL_OBJECT_INSTANCE_C_H #define SSOL_OBJECT_INSTANCE_C_H -#include <rsys/ref_count.h> #include <rsys/list.h> +#include <rsys/ref_count.h> +#include <rsys/str.h> struct ssol_object_instance { struct ssol_object* object; /* Instantiated object */ @@ -25,7 +26,7 @@ struct ssol_object_instance { struct s3d_shape* shape; /* Instantiated Star-3D shape */ struct list_node scene_attachment; - char* receiver_name; /* NULL if not a receiver */ + struct str receiver_name; /* Emptry if not a receiver */ struct ssol_device* dev; ref_T ref; diff --git a/src/test_ssol_object_instance.c b/src/test_ssol_object_instance.c @@ -58,8 +58,10 @@ main(int argc, char** argv) CHECK(ssol_object_instance_set_transform(instance, transform), RES_OK); CHECK(ssol_object_instance_set_receiver(NULL, "receiver 1"), RES_BAD_ARG); - CHECK(ssol_object_instance_set_receiver(instance, NULL), RES_BAD_ARG); + CHECK(ssol_object_instance_set_receiver(instance, NULL), RES_OK); CHECK(ssol_object_instance_set_receiver(instance, "receiver 1"), RES_OK); + CHECK(ssol_object_instance_set_receiver(instance, "receiver 0"), RES_OK); + CHECK(ssol_object_instance_set_receiver(instance, NULL), RES_OK); CHECK(ssol_object_instance_ref_put(instance), RES_OK);