| 1 | It is often good practice, especially when venturing on large API refactors, to double-check for any newly created memory leaks. |
| 2 | |
| 3 | Some leaks can only be spotted in external projects that show advance use cases of `rust-libxml`, for example allocating a `Node` in a default trait of a struct with a `Node` field. For now the only safe approach to that pattern is using the `Node::null()` placeholder, but the Rust idiomatic approach is to instead refactor to an `Option<Node>` field. |
| 4 | |
| 5 | Some, more direct, leak scenarios can already be spotted from the libxml test suite, and one can use valgrind to obtain a report via a call of the form: |
| 6 | |
| 7 | ``` |
| 8 | valgrind --leak-check=full target/debug/base_tests-3d29e5da1f969267 |
| 9 | ``` |
| 10 | |
| 11 | Additionally, as Rust nightlies keep evolving, a specific allocation system may be necessary to properly run valgrind. At the time of writing, `rust-libxml` tests need no such changes, but some external projects do. For convenience, here is a known working preamble, which can be added to the preambles of executable files, including example and test files. |
| 12 | |
| 13 | ```rust |
| 14 | #![feature(alloc_system, allocator_api)] |
| 15 | extern crate alloc_system; |
| 16 | use alloc_system::System; |
| 17 | |
| 18 | #[global_allocator] |
| 19 | static A: System = System; |
| 20 | ``` |
| 21 | |
| 22 | For more discussion motivating this explanation, see the respective [GitHub pull request](https://github.com/KWARC/rust-libxml/pull/43). |