Skip to content

Commit 5fff227

Browse files
authored
Merge pull request #262 from eclipse-zenoh/fix-259
fix memory leak in z_declare_subscriber
2 parents dd8b943 + db2ac4c commit 5fff227

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ include(GNUInstallDirs)
2121
option(BUILD_SHARED_LIBS "Build shared libraries if ON, otherwise build static libraries" ON)
2222
option(ZENOH_DEBUG "Use this to set the ZENOH_DEBUG variable." 0)
2323
option(WITH_ZEPHYR "Build for Zephyr RTOS" OFF)
24+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
25+
if(CMAKE_EXPORT_COMPILE_COMMANDS)
26+
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES
27+
${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
28+
endif()
2429

2530
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
2631
set(BUILD_SHARED_LIBS "OFF")

include/zenoh-pico/session/session.h

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ typedef void (*_z_data_handler_t)(const _z_sample_t *sample, void *arg);
9090

9191
typedef struct {
9292
_z_keyexpr_t _key;
93+
uint16_t _key_id;
9394
uint32_t _id;
9495
_z_data_handler_t _callback;
9596
_z_drop_handler_t _dropper;

src/api/api.c

+20-9
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,7 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z
890890
const z_subscriber_options_t *options) {
891891
void *ctx = callback->context;
892892
callback->context = NULL;
893+
char *suffix = NULL;
893894

894895
z_keyexpr_t key = keyexpr;
895896
// TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition
@@ -901,17 +902,24 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z
901902
_z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr);
902903
if (r == NULL) {
903904
char *wild = strpbrk(keyexpr._suffix, "*$");
905+
_Bool do_keydecl = true;
904906
if (wild != NULL && wild != keyexpr._suffix) {
905907
wild -= 1;
906908
size_t len = wild - keyexpr._suffix;
907-
char *suffix = z_malloc(len + 1);
908-
memcpy(suffix, keyexpr._suffix, len);
909-
suffix[len] = 0;
910-
keyexpr._suffix = suffix;
911-
_z_keyexpr_set_owns_suffix(&keyexpr, true);
909+
suffix = z_malloc(len + 1);
910+
if (suffix != NULL) {
911+
memcpy(suffix, keyexpr._suffix, len);
912+
suffix[len] = 0;
913+
keyexpr._suffix = suffix;
914+
_z_keyexpr_set_owns_suffix(&keyexpr, false);
915+
} else {
916+
do_keydecl = false;
917+
}
918+
}
919+
if (do_keydecl) {
920+
uint16_t id = _z_declare_resource(zs._val, keyexpr);
921+
key = _z_rid_with_suffix(id, wild);
912922
}
913-
uint16_t id = _z_declare_resource(zs._val, keyexpr);
914-
key = _z_rid_with_suffix(id, wild);
915923
}
916924
#if Z_FEATURE_MULTICAST_TRANSPORT == 1
917925
}
@@ -921,9 +929,12 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z
921929
if (options != NULL) {
922930
subinfo.reliability = options->reliability;
923931
}
932+
_z_subscriber_t *sub = _z_declare_subscriber(zs._val, key, subinfo, callback->call, callback->drop, ctx);
933+
if (suffix != NULL) {
934+
z_free(suffix);
935+
}
924936

925-
return (z_owned_subscriber_t){
926-
._value = _z_declare_subscriber(zs._val, key, subinfo, callback->call, callback->drop, ctx)};
937+
return (z_owned_subscriber_t){._value = sub};
927938
}
928939

929940
z_owned_pull_subscriber_t z_declare_pull_subscriber(z_session_t zs, z_keyexpr_t keyexpr,

src/net/primitives.c

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _
129129
_z_data_handler_t callback, _z_drop_handler_t dropper, void *arg) {
130130
_z_subscription_t s;
131131
s._id = _z_get_entity_id(zn);
132+
s._key_id = keyexpr._id;
132133
s._key = _z_get_expanded_key_from_key(zn, &keyexpr);
133134
s._info = sub_info;
134135
s._callback = callback;
@@ -175,6 +176,7 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) {
175176
_z_network_message_t n_msg = _z_n_msg_make_declare(declaration);
176177
if (_z_send_n_msg(sub->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) {
177178
// Only if message is successfully send, local subscription state can be removed
179+
_z_undeclare_resource(sub->_zn, s->ptr->_key_id);
178180
_z_unregister_subscription(sub->_zn, _Z_RESOURCE_IS_LOCAL, s);
179181
} else {
180182
ret = _Z_ERR_ENTITY_UNKNOWN;

zenohpico.pc

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
prefix=/usr/local
1+
prefix=/var/empty/local
22

33
Name: zenohpico
44
Description:
55
URL:
6-
Version: 0.11.20231012dev
6+
Version: 0.11.20231017dev
77
Cflags: -I${prefix}/include
8-
Libs: -L${prefix}/lib -lzenohpico
8+
Libs: -L${prefix}/lib64 -lzenohpico

0 commit comments

Comments
 (0)