具有线程的 MFC 应用程序中的内存泄漏
Memory leaks in MFC application with threads
任何人都可以帮助解决MFC应用程序中的内存泄漏吗?该程序似乎在没有以下代码块的情况下工作正常。该块包括有条件地执行多个任务和将数据传递到 MFC 对话框数据成员,然后更新 MFC 对话框上的指示器。其他测试显示一切正常,除了调试窗口中有内存泄漏消息。平台:WIN 7 64位,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 paralleln";
TRACE(message.c_str()); //Message in debugging window
}
代码
pvThread = threads.begin();
while (pvThread != threads.end())
{
pvThread->join();
pvThread++;
}
对我来说似乎有问题。第一次进入这个循环时,当前线程(我假设是主应用程序 UI 线程)在第一个线程上调用 join() 时将阻塞,直到该线程完成。 如果第一个线程至少比其他一些线程花费更长的时间,那么最终你会发现自己在一个失效的线程上调用 join()。 也许系统在处理此问题时泄漏了一些东西?
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- 为什么以下C++代码中存在内存泄漏?
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 我是否生成线程并导致内存泄漏?
- 多线程程序中出现意外的内存泄漏
- 为什么此函数会导致内存泄漏?
- 在 C++ 库中使用cythonized python时内存泄漏
- 需要帮助查找内存泄漏
- 瓦尔格林德的内存泄漏使用新的
- 无法找出我的代码中的内存泄漏
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 可视化 使用 VS Code 查找C++应用程序中的内存泄漏
- Shared_ptr双链接列表内存泄漏
- C++ 在类中使用常量引用文本时 O2 内存泄漏