带有线程的MFC应用程序中的内存泄漏

时间:2012-05-07 14:44:30

标签: c++ visual-c++ c++11

任何人都可以帮助MFC应用程序中的内存泄漏吗?没有以下代码块,该程序似乎工作正常。该块包括条件执行多个任务和将数据传递到MFC对话框数据成员,然后更新MFC对话框上的指示器。其他测试显示一切正常,除了调试窗口中有内存泄漏消息。平台:WIN 7 64bit,MSVC 2011.谢谢!

#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

}

1 个答案:

答案 0 :(得分:0)

代码

pvThread = threads.begin();
while (pvThread != threads.end())
{
     pvThread->join();
     pvThread++;
}

对我来说似乎有问题。第一次进入这个循环时,当前线程(我假设它是主应用程序UI线程)将在第一个线程上调用join()时阻塞,直到该线程完成。如果第一个线程花费的时间比至少其他一些线程长,那么最终你会发现自己在一个已经失效的线程上调用join()。也许系统在处理这个问题时会漏掉一些东西?