[CDRIVER-4452] Valgrind memory leak in bson_append_document Created: 08/Aug/22  Updated: 27/Oct/23  Resolved: 09/Aug/22

Status: Closed
Project: C Driver
Component/s: BSON
Affects Version/s: 1.22.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: iulian Assignee: Ezra Chung
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

When appending a bson document to a bsoncxx builder

valgrind reports a memory leak.

Environment

driver version: latest monogc latest mongocxx.

ubuntu 64 bit.

valgrind output:

==2064== 12,288 bytes in 10 blocks are definitely lost in loss record 618 of 620
==2064==    at 0x4C2DB4F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2064==    by 0x99536E: bson_malloc (bson-memory.c:66)
==2064==    by 0x985A98: _bson_impl_inline_grow (bson.c:128)
==2064==    by 0x985A98: _bson_grow (bson.c:219)
==2064==    by 0x986E03: _bson_append_va (bson.c:326)
==2064==    by 0x986E03: _bson_append (bson.c:412)
==2064==    by 0x9880F3: bson_append_document (bson.c:1147)
==2064==    by 0x977353: bsoncxx::v_noabi::builder::core::append(bsoncxx::v_noabi::types::b_document const&) 
==2064==    by 0x97A028: bsoncxx::v_noabi::builder::core::append(bsoncxx::v_noabi::document::view)

How to Reproduce

  1. have a bsoncxx::document::value  with data
  2. append the document from step 1 to a builder


 Comments   
Comment by iulian [ 09/Aug/22 ]

Hello.

based on your example i have tested with basic and stream builder and i can't get the memory leak to appear(if the last line is commented out).

I believe that i release the ownership somewhere deeper (as you have showed in the example).

I think yo can close the jira.

Thank you.

Comment by Ezra Chung [ 08/Aug/22 ]

Hello, tzulea. Thank you for this report.

Can you please provide a minimum example that reproduces this behavior?
Normal use of the bsoncxx::builder::core utility does not yield a leak as described:

#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/builder/core.hpp>
 
#include <iostream>
 
int main() {
  using bsoncxx::builder::basic::kvp;
 
  bsoncxx::builder::basic::document bson;
 
  // A "big" document that requires allocation.
  for (int i = 0; i < 100; ++i) {
    bson.append(kvp("a", 1));
  }
 
  bsoncxx::builder::core b = bsoncxx::builder::core(false);
 
  b.key_owned("x");
  b.append(bson); // bson_append_document -> _bson_impl_inline_grow
 
  bsoncxx::document::value doc = b.extract_document();
  bsoncxx::document::value::unique_ptr_type ptr = doc.release();
  std::uint8_t* data = ptr.release(); // <-- memory leak
}

I am only able to coerce the valgrind memory leak report exactly as you described by manually releasing ownership of the internal data of the resulting document and allowing it to leak. Can you please confirm the leak is not due to user error?

Generated at Wed Feb 07 21:20:58 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.