[CDRIVER-812] Memory Leak When Using bson_reinit() Created: 26/Aug/15 Updated: 19/Oct/16 Resolved: 09/Sep/15 |
|
| Status: | Closed |
| Project: | C Driver |
| Component/s: | docs, libbson |
| Affects Version/s: | 1.1.10 |
| Fix Version/s: | 1.2.0 |
| Type: | Improvement | Priority: | Minor - P4 |
| Reporter: | Peng Xie | Assignee: | A. Jesse Jiryu Davis |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Description |
|
The document said:
But this demo shows their difference:
Although outputs are the same when enable and disable USE_BSON_REINIT, valgrind detects a memory leak when USE_BSON_REINIT is true. Something like:
|
| Comments |
| Comment by Githook User [ 11/Jan/16 ] |
|
Author: {u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}Message: Merge branch '1.2.0-dev'
|
| Comment by Githook User [ 11/Jan/16 ] |
|
Author: {u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}Message: |
| Comment by Githook User [ 20/Oct/15 ] |
|
Author: {u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}Message: Merge branch '1.2.0-dev'
|
| Comment by Githook User [ 20/Oct/15 ] |
|
Author: {u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}Message: |
| Comment by Githook User [ 07/Oct/15 ] |
|
Author: {u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}Message: Merge branch '1.2.0-dev'
|
| Comment by Githook User [ 07/Oct/15 ] |
|
Author: {u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}Message: |
| Comment by Githook User [ 07/Oct/15 ] |
|
Author: {u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}Message: Merge branch '1.2.0-dev'
|
| Comment by Githook User [ 09/Sep/15 ] |
|
Author: {u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}Message: |
| Comment by A. Jesse Jiryu Davis [ 28/Aug/15 ] |
|
bson_copy_to has the right kind of guidance that bson_append_document_begin needs: |
| Comment by A. Jesse Jiryu Davis [ 27/Aug/15 ] |
|
To resolve this ticket, I'm going to update the docs for bson_reinit and for bson_append_document_begin. The bson_reinit will describe the stack vs. malloc'ed buffer distinction and note that bson_reinit doesn't free the buffer; you still must call bson_destroy to free it. If the API is used correctly then the current documentation is still accurate:
... but a more thorough note might've helped you understand the cause of your leak. I'll update the doc for bson_append_document_begin. Currently it just says:
... but that could much more prominently warn that "child" must not be initialized before passing it to bson_append_document_begin. |
| Comment by A. Jesse Jiryu Davis [ 27/Aug/15 ] |
|
I can understand your point that the documentation is incomplete. Here's what happens:
On the other hand, if in step 3 you destroy and init "bson" instead of bson_reinit, that actually spends the time freeing the malloc'ed buffer and reinitializing "bson" with the stack-based internal buffer, just as at the beginning. It's still technically incorrect to pass an initialized bson_t as the second argument to bson_append_document_begin, but in this second scenario it doesn't cause a leak because the bson_t has no malloc'ed buffer any more. |