Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
C ドライバー
/ /

bson_t の有効期間

bson_t には、そのデータが直接含まれる場合や、ヒープ割り当てメモリへのポインターが含まれている場合もあります。既存の bson_t を上書きするか、スタックに割り当てられた bson_t が範囲外になるようにするか、メモリ リークが発生する可能性があります。 bson_t は、常に bson_detry を使用して破棄する必要があります。

出力パラメーターとして使用される bson_t ポインターは、新しい bson_t の有効な上書き可能なストレージを点必要があり、次のいずれかである必要があります。

  • bson_t の非初期化ストレージ。

  • ゼロから初期化された bson_tオブジェクト。

  • bson_tオブジェクトは BSON_INITIALIZERで初期化されました。

  • bson_delete で破棄された bson_new で作成されなかった 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_detry を省略すると、メモリ リークが発生する可能性があります。

警告

bson_new から取得した bson_t ポインターを出力パラメーターとして渡すと、 bson_t 構造体のリークが発生します。

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

戻る

JSON

項目一覧