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 Lifetimes

bson_t는 데이터를 직접 포함하거나 힙 할당 메모리에 대한 포인터를 포함할 수 있습니다. 기존 bson_t를 덮어쓰거나 스택에 할당된 bson_t가 범위를 벗어나도록 허용하면 메모리 누수가 발생할 수 있습니다.bson_t는 항상 bson_destory로 파기해야 합니다.

출력 매개변수로 사용되는 bson_t 포인터는 다음 중 하나여야 하는 새 bson_t에 대한 유효한 덮어쓸 수 있는 저장 를 점 야 합니다.

  • bson_t의초기화되지 않은 저장 입니다.

  • 0으로 초기화된 bson_t 객체 입니다.

  • 로 초기화된 bson_t 객체 입니다. BSON_INITIALIZER

  • bson_new로 생성되지 않은 bson_t 객체 가 bson_destory로 삭제되었습니다.

이는 스택에 있을 수 있습니다.

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_destory를 생략하면 메모리 누수가 발생할 수 있습니다.

경고

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

이 페이지의 내용