---
name: "bson_valgrind"
section: "3"
platform: "linux"
package: "libbson-doc"
version: "1.23.1-1+deb12u3"
release: "Debian 12 (Bookworm)"
platforms: ["linux"]
url: "https://new.carta.tech/bson_valgrind"
---

# bson\_valgrind(3)

Use Valgrind to Check For BSON Data Leaks A stack-allocated bson\_t contains a small internal buffer; it only heap-allocates additional storage if necessary, depending on its data size. Therefore if you forget to call bson\_destroy() on a stack-allocated bson\_t, it might or might not cause a leak that can be detected by valgrind during testing. To catch all potential BSON data leaks in your code, configure the BSON\_MEMCHECK flag: With this flag set, every bson\_t mallocs at least one byte. Run your program's unittests with valgrind to verify all bson\_t structs are destroyed. Set the environment variable MONGOC\_TEST\_VALGRIND to on to skip timing-dependent tests known to fail with valgrind.

## NAME

bson\_valgrind - Use Valgrind to Check For BSON Data Leaks

A stack-allocated *bson\_t* contains a small internal buffer; it only heap-allocates additional storage if necessary, depending on its data size. Therefore if you forget to call *bson\_destroy()* on a stack-allocated *bson\_t*, it might or might not cause a leak that can be detected by valgrind during testing.

To catch all potential BSON data leaks in your code, configure the BSON\_MEMCHECK flag:

```
cmake -DCMAKE_C_FLAGS="-DBSON_MEMCHECK -g" .
```

With this flag set, every *bson\_t* mallocs at least one byte. Run your program's unittests with valgrind to verify all *bson\_t* structs are destroyed.

Set the environment variable **MONGOC\_TEST\_VALGRIND** to **on** to skip timing-dependent tests known to fail with valgrind.

## AUTHOR

MongoDB, Inc

## COPYRIGHT

2017-present, MongoDB, Inc
