Description
Describe the bug
I've produced a very minimal example to demonstrate serious memory leaks possibly from the FFI calls into/from Windows API. It could be win-rs or btelplug. But, I'll first report it here, since I am not sure.
To give a bit more context. We have a cross platform C++ app/library running on Microsoft Windows and GNU/Linux. We noticed (especially whenever we turn off the connected devices) we get noticable memory leaks. Initially we though our program or library leaks. Hopefully, we narrowed it down to the BLE code. Then, further investigation revealed it's not our C++ BLE wrapper or the Rust BLE code around the btleplug.
How are we sure? Well, we created a simple Rust library, copied the code from btleplug examples, then wrapped it inside a function called start_ble()
and exposed it via Rust FFI. Then we write a C++ program calling that function. The code is attached.
Why did we do that? Well, because we could use Windows memory leak trackers like Dr Memory, Visual Leak Tracker (a header file you include in your program), or even Deleaker (a Visual Studio / Qt Creator extention). All of them report the leaks from the Rust code. Screenshots for Deleaker are attached since it has a nice GUI to demonstrate it.
Expected behavior
The library should not leak memory on Windows. As Linux does not either.
Actual behavior
The program leaks memory, this is an issue if we make many calls to the underlying API. The OS evantually kills the app because of this, when we run out of memory.
Additional context
I've attached screenshots, a sample Rust / C++ program that you can use to confirm the leaks with any of the tools mentioned above. Also since the full binaries are over 1 GB. I'll only attach the soure files. You then can:
- Extract.
- Run cargo build to generate the FFI header and the .lib file.
- Open the .sln file in Visual Studio.
- Run.
You can obtain Deleaker with a trial license to see the leaks in Visual Studio. Also, you can obtain VLD or Dr Memory which are open source. For VLD you need some setup to include it and build. Feel free to let me know if you want to try it and I can set it up and upload another zip. Dr Memory you just drag the app into its shortcut.
Also, an XML file with all the leaks for this simple program is attached.