MONGOC_INIT_CLEANUP(3)

okay bestie, welcome to the mongoc_init_cleanup(3) page from the libmongoc manual ✨ yes, the entire page is about calling two functions at the right time, and honestly? for such tiny functions it's giving main character energy πŸ’… let's get into it.

✨ SYNOPSIS ✨

so here's the whole vibe: you initialize the MongoDB C Driver by calling mongoc_init() exactly once at the beginning of your program. that's it. that's the rule. it's responsible for initializing global state such as process counters, SSL, and threading primitives β€” lowkey the important stuff 🫢

exception alert 🚨 mongoc_log_set_handler() is the one that breaks the pattern β€” it should be called before mongoc_init(), otherwise some log traces would not use your log handling function. there's a detailed example in Custom Log Handlers, go look πŸ‘€

then, at the end of your program, you call mongoc_cleanup() exactly once to release all memory and other resources allocated by the driver. and after that? you must not call any other MongoDB C Driver functions. like, at all. periodt πŸ’€

also note β€” and this is the part people get wrong β€” mongoc_init() does not reinitialize the driver after mongoc_cleanup(). so don't think you can just fire it back up again, because no ma'am πŸ™ˆ

Changed in version 2.0.0: versions prior to 2.0.0 supported a non-portable automatic initialization and cleanup with the CMake option ENABLE_AUTOMATIC_INIT_AND_CLEANUP. that got removed in 2.0.0 πŸŽ€ so make sure your application calls mongoc_init() and mongoc_cleanup(). no cap, those are your two calls.


πŸ’– AUTHOR πŸ’–

MongoDB, Inc β€” iconic, we stan 🫢

2009-present, MongoDB, Inc.


and that's the page, bestie πŸ¦‹ this was libmongoc version 2.5.6, dated Oct 09, 2026 β€” now go call your init and cleanup functions in the right order, mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from