|
-
June 11th, 2010, 04:44 PM
#6
Re: Memory Leak within worker thread
To trace down the memory leak, start by commenting out all but the most basic code in the thread proc.
Code:
UINT ACC_MessageBox(PVOID p)
{
// Worker Thread Pass-In Variables (Channel & Alarm Message) -> i(int), ACC_ErrorBuffer(CString)
PPARAMS pParams = (PPARAMS)p;
/*
other code commented out
*/
delete pParams;
pParams = 0;
return (0);
}
Run this code and see if you have leaks (you shouldn't).
Next uncomment the code a line or two at a time and see if you reintroduce the memory leaks. When you find the lines that create the memory leaks, be sure to add appropriate cleanup (delete pFoo, etc.) code at the end of the thread proc.
Lastly, what would help in threading is to change your coding style to use all C++ code, rather than a combination of C++ and C-styled code.
Using pure C++, you can leverage the use of destructors and put any required cleanup code inside the destructor. For example, your thread proc may create a class instance on the stack within the thread proc. Inside the class it performs the work that you need and is also responsible for cleaning up any resources its allocated. When the thread exits, the class leaves scope and its destructor is called (which in turn frees up the resources).
That way you can avoid the error prone conditions where one section of code allocates resources and another is expected to free the resources.
__________________________________________________________
Arjay
See my latest series on using WCF to communicate between a Windows Service and WPF task bar application.
Tray Notify - Part I Tray Notify - Part II
Need a little help with Win32 thread synchronization? Check out the following CG articles and posts:
Sharing a thread safe std::queue between threads w/progress bar updating
Simple Thread: Part I Simple Thread: Part II
Win32 Thread Synchronization, Part I: Overview Win32 Thread Synchronization, Part 2: Helper Classes
www.iridyn.com
Last edited by Arjay; June 11th, 2010 at 04:47 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|