[CDRIVER-2241] Use a heap allocated bson_t as the 'child' in bson_append_xxx_begin without a memory leak Created: 09/Aug/17 Updated: 27/Oct/23 Resolved: 10/Aug/17 |
|
| Status: | Closed |
| Project: | C Driver |
| Component/s: | libbson |
| Affects Version/s: | 1.6.3 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Minor - P4 |
| Reporter: | Weilun Wang | Assignee: | Unassigned |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
Hi guys, Here is my old code:
When I run it with valgrind, valgrind detects memory leak:
I dig in a little. When I do bson_append_document_begin, _bson_append_bson_begin under the hood overwrites the flags of child to set BSON_FLAG_STATIC = 1. Later in bson_destroy, if BSON_FLAG_STATIC is set, bson_free will not be called. Right now, I am changing my code to:
No memory leak anymore. So my questions are: |
| Comments |
| Comment by Hannes Magnusson [ 10/Aug/17 ] |
|
See http://mongoc.org/libbson/current/creating.html#sub-documents-and-sub-arrays The child should not be allocated as it just points to the correct location in the parent. See also: http://mongoc.org/libbson/current/bson_append_document_begin.html#description
|