任何人都可以帮助解决MFC应用程序中的内存泄漏问题吗?该程序似乎没有下面的代码块正常工作。该块包括有条件地执行几个任务和将数据传递给MFC对话框数据成员,然后更新MFC对话框上的指示符。其他测试显示,除调试窗口中有内存泄漏消息外,一切正常。平台:WIN 7 64bit,MSVC 2011.谢谢!使用线程的MFC应用程序中的内存泄漏
#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
}
在发布的代码中似乎没有任何内存分配,所以内存泄漏几乎肯定会发生在您启动的其中一个线程运行的某个函数中。 – Chad
@ChadThanks乍得。我用下面的一个线程和一个简单的无所事事函数测试了代码,发现类似的内存泄漏,这让我想知道我是否有任何设置错误。我很奇怪,在实施结束时我收到了一条消息,比预期的要早。 void DoNothing()//测试函数 { \t //什么都不做 } – eLions
它看起来像我添加的线程与MFC对话框的线程发生冲突。顺便说一句,代码块可以在非GUI项目下运行就好了。 – eLions