C++ 带线程的MFC应用程序中的内存泄漏
有人能帮助解决MFC应用程序中的内存泄漏问题吗?如果没有下面的代码块,该程序似乎可以正常工作。该块包括多个任务的有条件执行和将数据传递给MFC对话框数据成员,然后更新MFC对话框上的指示器。其他测试显示一切正常,除了调试窗口中有内存泄漏消息。平台:Win7 64位,MSVC 2011。谢谢C++ 带线程的MFC应用程序中的内存泄漏,c++,visual-c++,c++11,C++,Visual C++,C++11,有人能帮助解决MFC应用程序中的内存泄漏问题吗?如果没有下面的代码块,该程序似乎可以正常工作。该块包括多个任务的有条件执行和将数据传递给MFC对话框数据成员,然后更新MFC对话框上的指示器。其他测试显示一切正常,除了调试窗口中有内存泄漏消息。平台:Win7 64位,MSVC 2011。谢谢 #include <vector> #include <thread> //Implementation parallel tasking void CMASTERDlg::OnC
#include <vector>
#include <thread>
//Implementation parallel tasking
void CMASTERDlg::OnCompositeParalleltasking()
{
const int totTsk=8;
BOOL select[totTsk]={
m_Parallel_Audio,
m_Parallel_DDS,
m_Parallel_HV,
m_Parallel_Monitor,
m_Parallel_PDA,
m_Parallel_Pulnix,
m_Parallel_Supertime,
m_Parallel_Temp};
//Put all selected tasks in a thread vector
std::vector<std::thread> threads;
auto pvThread = threads.begin();
if (m_Parallel_Audio)
threads.push_back(std::thread(Audio, 1));
if (m_Parallel_DDS)
threads.push_back(std::thread(DDS, 1, 1));
if (m_Parallel_HV)
threads.push_back(std::thread(HVgetShow, this, 3));
if (m_Parallel_Monitor)
threads.push_back(std::thread(MonitorgetShow, this));
if (m_Parallel_PDA)
threads.push_back(std::thread(PDAgetShow, this));
if (m_Parallel_Pulnix)
threads.push_back(std::thread(DoNothing, 1));
if (m_Parallel_Supertime)
threads.push_back(std::thread(MMCS,Sequence_id, static_cast<LPCSTR>(CStringA(loopnum))));
if (m_Parallel_Temp)
threads.push_back(std::thread(TempgetShow,this));
pvThread = threads.begin();
while (pvThread != threads.end())
{
pvThread->join();
pvThread++;
}
//update data on front panel
UpdateData(FALSE);
UpdateWindow();
//count selected tasks and output message
int j=0, count=0;
for(j=0; j<totTsk; j++) {
if (select[j]) count++;
}
char buffer[2];
itoa (count,buffer,10);
string message=string(buffer)+" tasks completed in parallel\n";
TRACE(message.c_str()); //Message in debugging window
}
代码
pvThread = threads.begin();
while (pvThread != threads.end())
{
pvThread->join();
pvThread++;
}
对我来说似乎有问题。第一次进入这个循环时,我假设是主应用程序UI线程的当前线程将在第一个线程上调用join时阻塞,直到该线程完成。如果第一个线程至少比其他一些线程花费更长的时间,那么最终您会发现自己在一个失效的线程上调用join。可能是系统在处理此问题时泄漏了什么?在发布的代码中似乎没有任何内存分配,因此几乎可以肯定内存泄漏发生在您启动的某个线程运行的某个函数中。@ChadThanks Chad。我用一个线程和一个简单的donothing函数测试了代码,如下所示,发现了类似的内存泄漏,这让我怀疑是否有任何设置错误。同样奇怪的是,我在实现的最后有一条消息,它比预期的更早弹出。void DoNothing//test function{//do nothing}看起来我添加的线程与用于MFC对话框的线程有冲突。顺便说一句,代码块可以在非GUI项目下正常运行。谢谢pnswdv。这似乎很可能解决问题。你有什么办法可以解决这个问题吗?检测到内存泄漏!正在转储0x00631518处的对象->{389}普通块,长56字节。数据:<>01 00 00 00 00 00 00 00对象转储完成。如果有人感兴趣,上述问题可通过MFC库中的AfxBeginThread解决,而不是直接使用线程。