ralfbiedert / interoptopus

The polyglot bindings generator for your library (C#, C, Python, …) 🐙
302 stars 27 forks source link

Explicit drop of Boxed context pointers in ffi_service and complex example context destruction #95

Closed michaeljm closed 9 months ago

michaeljm commented 9 months ago

The ffi_service macro generates code that raises unused_must_use warnings on build. That same pattern is used in the FFI function example_destroy_context.

   Compiling example_complex v0.1.1 (C:\Users\kin_s\prog\rust\interoptopus\examples\complex)
warning: unused return value of `Box::<T>::from_raw` that must be used
  --> examples\complex\src\ffi.rs:89:18
   |
89 |         unsafe { Box::from_raw(*ctx) };
   |                  ^^^^^^^^^^^^^^^^^^^
   |
   = note: call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`
   = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
   |
89 |         unsafe { let _ = Box::from_raw(*ctx); };
   |                  +++++++                    +

warning: unused return value of `Box::<T>::from_raw` that must be used
  --> reference_project\src\patterns\service.rs:20:6
   |
20 | impl SimpleService {
   |      ^^^^^^^^^^^^^
   |
   = note: call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`
   = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
   |
20 | impl let _ = SimpleService; {
   |      +++++++              +

warning: unused return value of `Box::<T>::from_raw` that must be used
   --> reference_project\src\patterns\service.rs:155:10
    |
155 | impl<'a> SimpleServiceLifetime<'a> {
    |          ^^^^^^^^^^^^^^^^^^^^^
    |
    = note: call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`
help: use `let _ = ...` to ignore the resulting value
    |
155 | impl<'a> let _ = SimpleServiceLifetime;<'a> {
    |          +++++++                      +

warning: `example_complex` (lib) generated 1 warning

To the best of my ability, the intent here was to drop the context object. The drop is happening with current code, so there's no memory leak. This is really about satisfying the compiler.

Because the warning occurs in generated code, I did not find a good way to use a targeted #[allow(unused_must_use)] for this issue. #![allow(unused_must_use)] worked, but applies to a very broad scope and could mask legit issues.

I recommend making the drop() explicit to remove the warning. This should not change the runtime behavior of the generated code otherwise.

ralfbiedert commented 9 months ago

Thanks!