“文档” 菜单
文档首页
/ / /
C 驱动程序
/ /

bson_t 生命周期

在此页面上

  • bson_t 输出参数

一个 bson_t 可以直接包含其数据,也可以包含指向堆分配内存的指针。覆盖现有的 bson_t 或允许堆栈分配的 bson_t 超出范围可能会导致内存泄漏。一个 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 这两种情况都可能导致内存泄漏。

警告

传递 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!

后退

JSON

来年

跨平台注释

在此页面上