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.
bson_t 출력 매개변수
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:
Uninitialized storage for a bson_t.
A zero-initialized bson_t object.
A bson_t object initialized with
BSON_INITIALIZER
.A bson_t object not created with bson_new that was destroyed with bson_destroy.
이는 스택에 있을 수 있습니다.
bson_t stack_doc = BSON_INITIALIZER; example_get_doc (&stack_doc); bson_destroy (&stack_doc);
또는 힙에서:
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.