BSON_LIFETIMES(3) | libbson | BSON_LIFETIMES(3) |
A bson_t may contain its data directly or may contain pointers to heap-allocated memory. Overwriting an existing bson_t or allowing a stack-allocated bson_t to go out of scope may cause a memory leak. A bson_t should always be destroyed with bson_destroy().
A bson_t pointer used as an out parameter must point to valid overwritable storage for a new bson_t which must be one of:
This can be on the stack:
bson_t stack_doc = BSON_INITIALIZER; example_get_doc (&stack_doc); bson_destroy (&stack_doc);
Or on the heap:
bson_t *heap_doc = bson_malloc (sizeof (bson_t)); example_get_doc (heap_doc); bson_destroy (heap_doc); bson_free (heap_doc);
Omitting bson_destroy() in either case may cause memory leaks.
WARNING:
bson_t *heap_doc = bson_new (); example_get_doc (heap_doc); bson_destroy (heap_doc); // Leaks the `bson_t` struct!
MongoDB, Inc
2017-present, MongoDB, Inc
March 31, 2024 | 1.26.0 |