bson_t 生命周期
在此页面上
一个 bson_t 可以直接包含其数据,也可以包含指向堆分配内存的指针。覆盖现有的 bson_t 或允许堆栈分配的 bson_t Go范围可能会导致内存泄漏。一个 bson_t 应始终使用 bson_destroy 销毁。
bson_t 输出参数
一个 bson_t 用作输出参数的指针必须点新的 bson_t 的有效可覆盖存储 必须是以下之一:
零初始化的 bson_t 对象。
一个 bson_t 使用
BSON_INITIALIZER
初始化的对象。一个 bson_t 对象不是使用 bson_new 创建的 已使用 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);
省略 bson_destroy 这两种情况都可能导致内存泄漏。