Skip to content

Commit 731d767

Browse files
committed
Fix memory leak of thread local storage.
1 parent c2eaea6 commit 731d767

6 files changed

Lines changed: 25 additions & 7 deletions

File tree

libs/error_injector/pthread/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ target_link_options(pthread_ei INTERFACE
2929
LINKER:--wrap,pthread_condattr_setpshared
3030
LINKER:--wrap,pthread_cond_init
3131
LINKER:--wrap,pthread_cond_timedwait
32+
LINKER:--wrap,pthread_key_create
3233
)
3334
add_library(Celix::pthread_ei ALIAS pthread_ei)

libs/error_injector/pthread/include/pthread_ei.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ CELIX_EI_DECLARE(pthread_cond_init, int);
4040

4141
CELIX_EI_DECLARE(pthread_cond_timedwait, int);
4242

43+
CELIX_EI_DECLARE(pthread_key_create, int);
44+
4345
#ifdef __cplusplus
4446
}
4547
#endif

libs/error_injector/pthread/src/pthread_ei.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,11 @@ int __wrap_pthread_cond_timedwait(pthread_cond_t * __cond, pthread_mutex_t * __m
7676
return __real_pthread_cond_timedwait(__cond, __mutex, __abstime);
7777
}
7878

79+
int __real_pthread_key_create(pthread_key_t * __key, void (*__destr_function)(void *));
80+
CELIX_EI_DEFINE(pthread_key_create, int)
81+
int __wrap_pthread_key_create(pthread_key_t * __key, void (*__destr_function)(void *)) {
82+
CELIX_EI_IMPL(pthread_key_create);
83+
return __real_pthread_key_create(__key, __destr_function);
84+
}
85+
7986
}

libs/utils/gtest/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ if (EI_TESTS)
113113
)
114114
target_link_libraries(test_utils_celix_err_key_create_with_ei PRIVATE
115115
utils_err_no_ctor_dtor_cut
116-
Celix::uv_ei
116+
Celix::pthread_ei
117117
GTest::gtest GTest::gtest_main
118118
)
119119
add_test(NAME test_utils_celix_err_key_create_with_ei COMMAND test_utils_celix_err_key_create_with_ei)

libs/utils/gtest/src/ErrKeyCreateErrorInjectionTestSuite.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@
2121

2222
#include "celix_err.h"
2323
#include "celix_err_private.h"
24-
#include "uv_ei.h"
24+
#include "pthread_ei.h"
2525

2626
class ErrKeyCreateErrorInjectionTestSuite : public ::testing::Test {
2727
public:
2828
ErrKeyCreateErrorInjectionTestSuite() = default;
2929

3030
~ErrKeyCreateErrorInjectionTestSuite() noexcept override {
31-
celix_ei_expect_uv_key_create(nullptr, 0, 0);
31+
celix_ei_expect_pthread_key_create(nullptr, 0, 0);
3232
}
3333
};
3434

3535
TEST_F(ErrKeyCreateErrorInjectionTestSuite, InitThreadStorageKeyFailureAndSuccess) {
36-
celix_ei_expect_uv_key_create((void*)celix_err_initThreadSpecificStorageKey, 0, -1);
36+
celix_ei_expect_pthread_key_create((void*)celix_err_initThreadSpecificStorageKey, 0, -1);
3737
celix_err_initThreadSpecificStorageKey();
3838

3939
celix_err_push("error message");
4040
EXPECT_EQ(0, celix_err_getErrorCount());
4141

42-
celix_ei_expect_uv_key_create(nullptr, 0, 0);
42+
celix_ei_expect_pthread_key_create(nullptr, 0, 0);
4343
celix_err_initThreadSpecificStorageKey();
4444
celix_err_resetErrors();
4545
celix_err_push("error message");

libs/utils/src/celix_err.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <stdio.h>
2525
#include <string.h>
2626
#include <stdlib.h>
27+
#include <pthread.h>
2728
#include <uv.h>
2829

2930

@@ -44,6 +45,13 @@ typedef struct celix_err {
4445
uv_key_t celix_err_tssKey;
4546
bool celix_err_tssKeyInitialized = false;
4647

48+
static void celix_err_destroyTssErr(void* data) {
49+
celix_err_t* err = data;
50+
if (err != NULL) {
51+
free(err);
52+
}
53+
}
54+
4755
static celix_err_t* celix_err_getRawTssErr() {
4856
if (!celix_err_tssKeyInitialized) {
4957
return NULL;
@@ -77,11 +85,11 @@ CELIX_ERR_CONSTRUCTOR void celix_err_initThreadSpecificStorageKey() {
7785
if (celix_err_tssKeyInitialized) {
7886
return;
7987
}
80-
int rc = uv_key_create(&celix_err_tssKey);
88+
int rc = pthread_key_create(&celix_err_tssKey, celix_err_destroyTssErr);
8189
if (rc == 0) {
8290
celix_err_tssKeyInitialized = true;
8391
} else {
84-
fprintf(stderr,"Failed to create thread specific storage key for celix_err\n");
92+
fprintf(stderr,"Failed to create thread specific storage key for celix_err %d\n", rc);
8593
}
8694
}
8795

0 commit comments

Comments
 (0)