diff --git a/bindings/rust/evmc-declare/src/lib.rs b/bindings/rust/evmc-declare/src/lib.rs index b3ae01e..7a64d2e 100644 --- a/bindings/rust/evmc-declare/src/lib.rs +++ b/bindings/rust/evmc-declare/src/lib.rs @@ -301,8 +301,11 @@ fn build_create_fn(names: &VMNameSet) -> proc_macro2::TokenStream { version: unsafe { ::std::ffi::CStr::from_bytes_with_nul_unchecked(#static_version_ident.as_bytes()).as_ptr() as *const i8 }, }; + let container = ::evmc_vm::EvmcContainer::<#type_ident>::new(new_instance); + unsafe { - ::evmc_vm::EvmcContainer::into_ffi_pointer(Box::new(::evmc_vm::EvmcContainer::<#type_ident>::new(new_instance))) + // Release ownership to EVMC. + ::evmc_vm::EvmcContainer::into_ffi_pointer(container) } } } @@ -319,6 +322,7 @@ fn build_destroy_fn(names: &VMNameSet) -> proc_macro2::TokenStream { std::process::abort(); } unsafe { + // Acquire ownership from EVMC. This will deallocate it also at the end of the scope. ::evmc_vm::EvmcContainer::<#type_ident>::from_ffi_pointer(instance); } } @@ -367,6 +371,7 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream { }; let container = unsafe { + // Acquire ownership from EVMC. ::evmc_vm::EvmcContainer::<#type_name_ident>::from_ffi_pointer(instance) }; @@ -385,6 +390,7 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream { }; unsafe { + // Release ownership to EVMC. ::evmc_vm::EvmcContainer::into_ffi_pointer(container); } diff --git a/bindings/rust/evmc-vm/src/container.rs b/bindings/rust/evmc-vm/src/container.rs index fc3db1b..6565ed5 100644 --- a/bindings/rust/evmc-vm/src/container.rs +++ b/bindings/rust/evmc-vm/src/container.rs @@ -22,11 +22,11 @@ where T: EvmcVm + Sized, { /// Basic constructor. - pub fn new(_instance: ::evmc_sys::evmc_instance) -> Self { - Self { + pub fn new(_instance: ::evmc_sys::evmc_instance) -> Box { + Box::new(Self { instance: _instance, vm: T::init(), - } + }) } /// Take ownership of the given pointer and return a box. @@ -147,7 +147,7 @@ mod tests { ::evmc_sys::evmc_status_code::EVMC_FAILURE ); - let ptr = unsafe { EvmcContainer::into_ffi_pointer(Box::new(container)) }; + let ptr = unsafe { EvmcContainer::into_ffi_pointer(container) }; let mut context = ExecutionContext::new(&mut backing_context); let container = unsafe { EvmcContainer::::from_ffi_pointer(ptr) };