没有删除调用的 ManuallyDrop 会不会导致内存泄漏?

问题描述

我正在阅读 wasm-bindgen 指南,我遇到了它为 jsrust间的交互生成glue code。对值的引用从 js 传递给 Rust。 Rust 必须将它包装在 ManuallyDrop 中,以便它不会调用Drop 上实现的 JsValue

pub fn foo(a: &JsValue) {
    // ...
}

#[export_name = "foo"]
pub extern "C" fn __wasm_bindgen_generated_foo(arg0: u32) {
    let arg0 = unsafe {
        ManuallyDrop::new(JsValue::__from_idx(arg0))
    };
    let arg0 = &*arg0;
    foo(arg0);
}

但我没有看到在 ManuallyDrop::drop调用arg0。那么除非调用 JsValue 函数,否则包装在 ManuallyDrop 中的 ManuallyDrop::drop(arg0) 会被删除吗?会不会造成内存泄漏?

解决方法

01/19/2021 08:33:51 AM - Process(24037.1) User(mqm) Program(TestConnect.pl) Host(localhost.localdomain) Installation(Installation1) VRMF(9.1.0.1) Time(2021-01-19T08:33:51.725Z) RemoteHost(10.132.4.9(1414)) CommentInsert1(CHANNEL_TLS) CommentInsert2(host1 (10.132.4.9)(1414)) AMQ9642E: No SSL or TLS certificate for channel 'CHANNEL_TLS'. EXPLANATION: The channel 'CHANNEL_TLS' did not supply a certificate to use during SSL or TLS handshaking,but a certificate is required by the remote queue manager. The remote host is 'host1 (10.132.4.9)(1414)'. The channel did not start. ACTION: Ensure that the key repository of the local queue manager or MQ client contains a certificate which is associated with the queue manager or client. If you have configured a certificate label,check that the certificate exists. Alternatively,if appropriate,change the remote channel definition so that its SSLCAUTH attribute is set to OPTIONAL and it has no SSLPEER value set. ----- cmqxrfpt.c : 690 -------------------------------------------------------- 不会阻止内部值被销毁。它只会阻止 ManuallyDrop 被调用。考虑一个drop

Vec

字段 pub struct Vec<T> { ptr: *mut T,cap: usize,len: usize,} ptrcap仍然被销毁,即使被 len 包裹。但是,由于未调用 ManuallyDrop,因此不会释放管理的任何动态资源(在本例中为 ptr 引用的数据)。

由于 drop 只是持有一个 JsValue,因此 Rust 端不会发生泄漏。由于胶水代码可确保对 u32 参数进行适当的清理,因此 Javascript 端不会泄漏内存。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...