Click here to Skip to main content
15,920,801 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Windows Shutdown Pin
hxhl9514-Mar-09 9:49
hxhl9514-Mar-09 9:49 
GeneralRe: Windows Shutdown [modified] Pin
NeoAks00714-Mar-09 10:10
NeoAks00714-Mar-09 10:10 
GeneralRe: Windows Shutdown [modified] Pin
hxhl9514-Mar-09 12:08
hxhl9514-Mar-09 12:08 
Question[Message Deleted] Pin
mini_25068914-Mar-09 7:56
mini_25068914-Mar-09 7:56 
AnswerRe: help me!!!!!!!! Pin
Stuart Dootson14-Mar-09 8:00
professionalStuart Dootson14-Mar-09 8:00 
GeneralRe: help me!!!!!!!! Pin
NeoAks00714-Mar-09 8:04
NeoAks00714-Mar-09 8:04 
AnswerRe: help me!!!!!!!! Pin
NeoAks00714-Mar-09 8:02
NeoAks00714-Mar-09 8:02 
QuestionHow to delete handles of a thread created with AfxBeginThread Pin
keret14-Mar-09 7:29
keret14-Mar-09 7:29 
I have an MFC dialog application. It monitors certain types of windows and collects information on them. I start a new thread for every window based on it's title. I use a timer to find windows that meet the criteria. I created a struct named ThreadInfo and I pass it to the thread. At the end I either close the monitored window(s) or close my dialogs.
This causes several memory leaks.

Here is the code in the timer if it finds a window that meets the criteria:

//Create ThreadInfo
    ThreadInfo *pInfo = new ThreadInfo;
   
    //put the information into pInfo
    pInfo->hMainWnd = dlg->GetSafeHwnd();
    pInfo->hMonitoredWnd = hwnd;
    pInfo->x = y; //And so on...

    //Start thread
    CWinThread *pThread = AfxBeginThread(ThreadFunction, (PVOID) pInfo, THREAD_PRIORITY_NORMAL, 0, 0);


Here is the code for ThreadFunction:

UINT ThreadFunction(LPVOID pParam)
{
    ThreadInfo *pInfo = reinterpret_cast<threadinfo> (pParam);
 
    while (IsWindow(pInfo->hMonitoredWindow))
    {
      
       do.Something;
       Sleep(100);
       
    }
    //delete pInfo;
    return 0;
}</threadinfo>


I tried the following scenarions to eliminate or at least identify the memory leaks. The resulting leaks are below them:

A:
- I create pInfo and close the dialog first:
thrcore.cpp 68B client block
mydlg.cpp 748B normal block
strcore.cpp 46B normal block

B:
- I create pInfo and close the monitored window first:
mydlg.cpp 748B nromal block
strcore.cpp 46B normal block

C:
- I create pInfo, but don't start the thread:
mydlg.cpp 748B normal block
strcore.cpp 46B normal block

D:
- I don't create pInfo and pass NULL to the thread:
no leaks!

E:
- I delete pInfo in ThreadFunction and close monitored window first:
no cpp file, 160B normal block

F:
- I delete pInfo in ThreadFunction and close dialog first:
no cpp file, 160B normal block
thrcore.cpp 68B client block
mydlg.cpp 748B normal block
strcore.cpp 46B normal block

So I think the 748B and 46B blocks are for pInfo, while the 68B and 160B block are for pThread. But how and where should I delete them?
I think I should delete them in the dialog, but how do I send a message from the thread when it ends? And how I delete the handles if I close the dialog first?
AnswerRe: How to delete handles of a thread created with AfxBeginThread Pin
Stuart Dootson14-Mar-09 7:58
professionalStuart Dootson14-Mar-09 7:58 
GeneralRe: How to delete handles of a thread created with AfxBeginThread Pin
keret14-Mar-09 9:51
keret14-Mar-09 9:51 
GeneralRe: How to delete handles of a thread created with AfxBeginThread Pin
Stuart Dootson14-Mar-09 10:36
professionalStuart Dootson14-Mar-09 10:36 
GeneralRe: How to delete handles of a thread created with AfxBeginThread Pin
keret14-Mar-09 11:17
keret14-Mar-09 11:17 
GeneralRe: How to delete handles of a thread created with AfxBeginThread Pin
Stuart Dootson14-Mar-09 11:52
professionalStuart Dootson14-Mar-09 11:52 
GeneralRe: How to delete handles of a thread created with AfxBeginThread Pin
keret15-Mar-09 4:03
keret15-Mar-09 4:03 
GeneralRe: How to delete handles of a thread created with AfxBeginThread Pin
Stuart Dootson15-Mar-09 6:07
professionalStuart Dootson15-Mar-09 6:07 
GeneralRe: How to delete handles of a thread created with AfxBeginThread Pin
keret15-Mar-09 9:55
keret15-Mar-09 9:55 
QuestionTemplate Class Issues in C++. Pin
NeoAks00714-Mar-09 7:24
NeoAks00714-Mar-09 7:24 
AnswerRe: Template Class issues in C++. Pin
N a v a n e e t h14-Mar-09 7:31
N a v a n e e t h14-Mar-09 7:31 
GeneralRe: Template Class issues in C++. Pin
NeoAks00714-Mar-09 7:38
NeoAks00714-Mar-09 7:38 
GeneralRe: Template Class issues in C++. Pin
N a v a n e e t h14-Mar-09 7:45
N a v a n e e t h14-Mar-09 7:45 
QuestionRe: Template Class issues in C++. Pin
NeoAks00714-Mar-09 7:50
NeoAks00714-Mar-09 7:50 
AnswerRe: Template Class issues in C++. Pin
Stuart Dootson14-Mar-09 8:07
professionalStuart Dootson14-Mar-09 8:07 
AnswerRe: Template Class issues in C++. Pin
N a v a n e e t h14-Mar-09 8:14
N a v a n e e t h14-Mar-09 8:14 
QuestionRe: Template Class issues in C++. Pin
NeoAks00714-Mar-09 8:32
NeoAks00714-Mar-09 8:32 
NewsRe: Template Class issues in C++. Pin
NeoAks00714-Mar-09 9:16
NeoAks00714-Mar-09 9:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.