gpt4 book ai didi

c++ - 在一个线程中新建并在另一个线程中删除,不允许吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:26:47 26 4
gpt4 key购买 nike

我正在从 com 端口读取数据。因为不知道什么时候有数据,所以一直在一个线程里读。

当我读取了足够多的字节后,我通过发布一 strip 有指向字符串指针的消息让主线程知道这一点:

msg[i] = '\0';
completeMsg = new char[i];
strcpy(completeMsg, msg);
PostMessage(hDlg, BT_MSG, NULL, (LPARAM) completeMsg);
i = 0;

主线程对此消息的响应是:

case BT_MSG:
{
char* msg = (char*) lParam;
ShowMsg(msg);
delete [] msg;
break;
}

但似乎不允许在此线程中删除,因为我在执行删除行时收到此错误:

Windows has triggered a breakpoint in SPO.exe.

This may be due to a corruption of the heap, which indicates a bug in SPO.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while SPO.exe has focus.

The output window may have more diagnostic information.

我应该使用一些全局变量还是发回消息让读取线程处理删除?它没有消息循环自动取款机,所以我不想为此添加一个。

最佳答案

您应该能够在一个线程中使用 new 并在另一个线程中使用 delete,前提是您链接到编译器的多线程运行时库。

但是,看起来您实际上遇到了缓冲区溢出。您使用 msg[i]=0 以 null 终止 msg,但只分配 i 字节 --- 您可能需要 new字符[i+1].

关于c++ - 在一个线程中新建并在另一个线程中删除,不允许吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5608199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com