Skip to content

Commit 5a060d3

Browse files
committed
Avoid the memory leak in the add_job function
1. When allocating memory for future, if the allocation fails, do not simply return NULL. Instead, release the memory allocated for job beforehand to avoid memory leaks.
1 parent d9dd152 commit 5a060d3

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

examples/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ all:
33
clean:
44
rm -f rmw_example
55
check: all
6-
./rmw_example
6+
./rmw_example

examples/rmw_example.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ struct tpool_future *add_job(tpool_t *thrd_pool, void *(*func)(void *),
191191
return NULL;
192192

193193
struct tpool_future *future = tpool_future_create();
194-
if (!future)
194+
if (!future){
195+
free(job);
195196
return NULL;
197+
}
196198

197199
// unprotected producer
198200
job->args = args;

0 commit comments

Comments
 (0)