小编Anu*_*hra的帖子

为什么这个程序中存在内存泄漏?在给定约束的情况下如何解决它(对包含 std::string 的对象使用 malloc 和 free)?

这是我在实际代码中遇到的问题的最小工作示例。

#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 个字节


约束条件

  1. 我一定会malloc在上面的示例中使用,因为在我的实际代码中,我在 C++ 项目中使用了 C 库,该项目malloc在内部使用了它。所以我无法摆脱malloc使用,因为我没有在代码中的任何地方明确地这样做。
  2. 我需要在我的代码中一次又一次地重新std::string name分配。Person

c++ valgrind memory-leaks placement-new dynamic-memory-allocation

58
推荐指数
5
解决办法
5415
查看次数

异步函数中await 可以等待多长时间?

我想知道,异步函数中将等待多长时间[从而将所有资源保留在 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] 多长时间?等待也有超时,或者等待实际上可以无限等待吗?【如何控制这个超时时间】

javascript async-await

8
推荐指数
2
解决办法
9342
查看次数

使用自签名证书在 Deno 上设置 HTTPS?

我正在设置 Deno 服务器来处理 HTTPS 请求,我使用自签名证书来完成这项工作。\n为此使用了以下代码:

\n\n
import { 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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我运行此代码为:deno --allow-net --allow-read app.ts\n我收到以下错误:

\n\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)

deno

5
推荐指数
1
解决办法
3537
查看次数

如何让网络工作者同时执行多项任务?

我试图让 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 …

javascript web-worker node.js typescript deno

1
推荐指数
1
解决办法
2286
查看次数