这是我在实际代码中遇到的问题的最小工作示例。
#include <iostream>
namespace Test1 {
static const std::string MSG1="Something really big message";
}
struct Person{
std::string name;
};
int main() {
auto p = (Person*)malloc(sizeof(Person));
p = new(p)Person();
p->name=Test1::MSG1;
std::cout << "name: "<< p->name << std::endl;
free(p);
std::cout << "done" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译它并通过Valgrind运行它时,它给了我这个错误:
肯定丢失:1 个块中 31 个字节
malloc在上面的示例中使用,因为在我的实际代码中,我在 C++ 项目中使用了 C 库,该项目malloc在内部使用了它。所以我无法摆脱malloc使用,因为我没有在代码中的任何地方明确地这样做。std::string name分配。Personc++ valgrind memory-leaks placement-new dynamic-memory-allocation
我想知道,异步函数中将等待多长时间[从而将所有资源保留在 RAM 中]。考虑这个例子:
async function my_func(some_big_sized_data){
let db_input = await gettingDBinputWhichIsDamnedSlow();
//then do some processing based on db_input and some_big_sized_data
}
Run Code Online (Sandbox Code Playgroud)
现在,如果 DB 总是花很长时间才回复怎么办?wait 函数将等待并保存所有这些数据[并在此过程中占用 RAM] 多长时间?等待也有超时,或者等待实际上可以无限等待吗?【如何控制这个超时时间】
我正在设置 Deno 服务器来处理 HTTPS 请求,我使用自签名证书来完成这项工作。\n为此使用了以下代码:
\n\nimport { serveTLS } from "https://deno.land/std/http/server.ts";\n\nconst body = new TextEncoder().encode("Hello HTTPS");\nconst options = {\n hostname: "localhost",\n port: 443,\n certFile: "./path/to/localhost.crt",\n keyFile: "./path/to/localhost.key",\n};\n// Top-level await supported\nfor await (const req of serveTLS(options)) {\n req.respond({ body });\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我运行此代码为:deno --allow-net --allow-read app.ts\n我收到以下错误:
ERROR RS - rustls::session:571 - TLS alert received: Message {\n typ: Alert,\n version: TLSv1_3,\n payload: Alert(\n AlertMessagePayload {\n level: Fatal,\n description: BadCertificate,\n },\n ),\n}\nerror: Uncaught InvalidData: received fatal alert: BadCertificate\n\xe2\x96\xba $deno$/errors.ts:57:13\n at …Run Code Online (Sandbox Code Playgroud) 我试图让 Web Worker 管理其状态,同时服务多个异步请求。
工人.ts 文件
let a =0; //this is my worker's state
let worker=self as unknown as Worker;
worker.onmessage =(e)=>{
console.log("Rec msg", e.data);
if(e.data === "+1"){
setTimeout(()=>{
a=a+1;
worker.postMessage(a);
},3000);
}else if(e.data=== "+2"){
setTimeout(()=>{
a=a+2;
worker.postMessage(a);
},1000)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的主文件:main.ts
let w =new Worker("./worker.ts", {type: "module"})
let wf =async (op: string)=>{
w.postMessage(op);
return new Promise<any>((res,rej)=>{
w.onmessage=res;
});
}
(async()=>{
let f1 = await wf("+1");
console.log("f1",f1.data);
})();
(async()=>{
let f2 = await wf("+2");
console.log("f2",f2.data);
})()
Run Code Online (Sandbox Code Playgroud)
只能f2归还,就f1 …
deno ×2
javascript ×2
async-await ×1
c++ ×1
memory-leaks ×1
node.js ×1
typescript ×1
valgrind ×1
web-worker ×1