Is the following usage of pointers in functions call is a memory leak:
bson_t * parse_json(const char * json_fields){
bson_error_t error;
bson_t *bson_fields = bson_new_from_json((unsigned char *)json_fields, -1, &error);
if (!bson_fields) {
log_die("Error: %s\n", error.message);
} else {
return bson_fields;
}
log_die("Error: something bad happend in parse_columns");
return bson_fields; // this should never be reached ...
}
The following code works, but what happens to the pointer from parse_json
here? Is this a memory leak?
bson_concat(fields, parse_json(json_fields));
The mongodb C-API offers the function bson_destory
:
bson_destroy(fields);
I am wondering maybe it's better to explicitly free the memory of new_fields
:
bson_t *new_fields = parse_json(json_fields);
bson_concat(fields, new_fields);
bson_destroy(new_fields);
While this example uses mongodb c-api, I am also trying to understand the general case.
some_type * pointer_returner(){
some_type *var;
...
return var;
}
do_something(pointer_retuner());
Is the call above causing a memory leak?
Yes, you need to call bson_destroy
to deallocate your structure object it is no longer used.
From bson_destroy
documentation:
The bson_destroy() function shall free an allocated bson_t structure.
This function should always be called when you are done with a bson_t unless otherwise specified.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments