2012-05-07 26 views
0

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 

} 
+1

投稿されたコードにメモリ割り当てがないように見えるので、起動したスレッドの1つで実行される関数の1つでメモリリークが発生する可能性はほとんどありません。 – Chad

+0

@ChadThanksチャド。私はちょうど1つのスレッドと以下のような単純なdo-nothing関数を使ってコードをテストし、同様のメモリリークを見たので、設定が間違っていたのか不思議でした。実装の最後に、予想よりも早く予期したメッセージが出てしまったのも不思議でした。 void DoNothing()// test function { \t // do nothing } – eLions

+0

追加したスレッドは、MFCダイアログのスレッドと競合しているようです。ところで、コードブロックは非GUIプロジェクトの下でうまく動作することができます。 – eLions

答えて

0

コード

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

は、私には疑問そうです。このループの最初の時点で、現在のスレッド(メインのアプリケーションUIスレッドと見なします)は、そのスレッドが完了するまで、最初のスレッドでjoin()を呼び出すとブロックされます。最初のスレッドが他のスレッドの少なくとも一部よりも時間がかかる場合、最終的には機能していないスレッドでjoin()を呼び出すことになります。おそらく、システムはこれを処理しながら何かを漏らしていますか?

+0

ありがとうpnswdv。これは問題の可能性が非常に高いようです。これを解決するヒントはありますか?検出されたメモリリーク! オブジェクトのダンプ - > {389}通常ブロック(0x00631518、56バイト)。 データ:<> 01 00 00 00 00 00 00 00 00 00 00 00 00 00 オブジェクトのダンプ完了。 – eLions

+1

誰かが興味がある場合は、上記の問題は、直接スレッドを使用するのではなく、MFCライブラリからAfxBeginThreadで解決されます。 – eLions

関連する問題