Docs 菜单

bson_t 生命周期

在此页面上

一个 bson_t 可以直接包含其数据,也可以包含指向堆分配内存的指针。覆盖现有的 bson_t 或允许堆栈分配的 bson_t Go范围可能会导致内存泄漏。一个 bson_t 应始终使用 bson_destroy 销毁。

一个 bson_t 用作输出参数的指针必须点新的 bson_t 的有效可覆盖存储 必须是以下之一:

这可以在堆栈上:

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 这两种情况都可能导致内存泄漏。

警告

传递 bson_t bson_new 获得的指针 作为输出参数会导致 bson_t 泄漏 struct。

bson_t *heap_doc = bson_new ();
example_get_doc (heap_doc);
bson_destroy (heap_doc); // Leaks the `bson_t` struct!

在此页面上