Discussion:
Memory Leak in MFC?
(too old to reply)
Georg Steigmeier
2009-03-03 11:07:01 UTC
Permalink
Hi
I have a strange memory leak in my application running under CE 6.0. My
investigations showed that it has something to do with ActiveX controls and
starting/stopping threads.
Now I made a test application that has the same problem. It's a simple
dialog based application that uses MFC and has an ActiveX control on it
(application and ActiveX generated with the wizard). The application and the
ActiveX use MFC in a shared DLL. Now if a start a worker thread e.g. in an
event handler inside the dialog class, then I see that the available memory
shrinks (about 1600 bytes) after the thread stops. It doesn' matter if I use
CreateThread or AfxBeginThread to start the thread. Note that I don' do
anything inside the thread function, it just returns.
I don't have this problem if I remove the (dummy) ActiveX from the dialog.
There is also no leak if the ActiveX uses MFC in a static library.

Is there a problem inside MFC?
Georg Steigmeier
2009-03-04 10:49:02 UTC
Permalink
By the way, I am using Visual Studio 2005 and therefore MFC 8.0.
Anthony Wieser
2009-03-05 14:09:36 UTC
Permalink
Are you cleaning up the thread handle?

Anthony Wieser
Wieser Software Ltd
Post by Georg Steigmeier
By the way, I am using Visual Studio 2005 and therefore MFC 8.0.
Georg Steigmeier
2009-03-06 07:31:03 UTC
Permalink
Hi Anthony

Yes, the thread handles are closed. Another thing is, that the memory leak
only occurs under windows CE and not under Win32. I think it's something
inside MFC.
Post by Anthony Wieser
Are you cleaning up the thread handle?
Anthony Wieser
Wieser Software Ltd
Chris Tacke, eMVP
2009-03-06 20:14:50 UTC
Permalink
You have the full source to MFC....

In Studio 08 it's here:
C:\Program Files\Microsoft Visual Studio 9.0\VC\ce\atlmfc\src\mfc

For '05 it was something very similar (don't have it installed to check).
--
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
Post by Georg Steigmeier
Hi Anthony
Yes, the thread handles are closed. Another thing is, that the memory leak
only occurs under windows CE and not under Win32. I think it's something
inside MFC.
Post by Anthony Wieser
Are you cleaning up the thread handle?
Anthony Wieser
Wieser Software Ltd
Georg Steigmeier
2009-03-09 15:26:06 UTC
Permalink
Hi Chris

I find it quite difficult to debug the MFC internals. I tried to find
different behaviours between Win32 and WinCE, because the memory leak does
not occur under Win32. This is one thing I found:

If my “worker” thread (created with CreateThread) ends, then I see that the
function CThreadSlotData::DeleteValues(CThreadData* pData, HINSTANCE hInst)
[afxtls.cpp] is called from the MFC DLL and from my ActiceX control.

- Under Win32 the DeleteValues() function is first called from my ActiveX
and here no delete operation really takes place, then it is called from the
MFC DLL and the thread data gets deleted.

- Under WinCE the DeleteValues() function is called in different order.
First from the MFC DLL and the thread data gets already deleted, then it is
called from my ActiveX and here no delete operation takes place anymore. But
in between it seems that new thread data is created through calls to
CThreadSlotData::SetValue(int nSlot, void* pValue).

Do you know if the behaviour under WinCE is wrong?

Thanks in advance
Georg
Post by Chris Tacke, eMVP
You have the full source to MFC....
C:\Program Files\Microsoft Visual Studio 9.0\VC\ce\atlmfc\src\mfc
For '05 it was something very similar (don't have it installed to check).
--
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
Bill T
2009-03-06 14:54:14 UTC
Permalink
We had very similar behavior with CE5.0, and that MSFT recommended to us to
preload the MFC dll "early in the WinMain. This did correct the memory leak
we were seeing.

extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
{
....

LoadLibrary(_T("mfcce400.dll"));

...
}
Post by Georg Steigmeier
By the way, I am using Visual Studio 2005 and therefore MFC 8.0.
Georg Steigmeier
2009-03-10 08:01:04 UTC
Permalink
I also tried it with Visual Studio 2008 and therefore MFC 9.0. The memory
leak problem still exists.
Post by Georg Steigmeier
By the way, I am using Visual Studio 2005 and therefore MFC 8.0.
Georg Steigmeier
2009-03-31 13:15:03 UTC
Permalink
Here is my code to reproduce the memory leak:

http://cid-1e808bf3e2edf989.skydrive.live.com/self.aspx/.Public/MemLeakThreadProblem.zip

Any help is appreciated!
Helge Kruse
2009-05-19 14:35:43 UTC
Permalink
Is there any other place where you can store the ZIP file. I found no way
to open it.

Helge
Post by Georg Steigmeier
http://cid-1e808bf3e2edf989.skydrive.live.com/self.aspx/.Public/MemLeakThreadProblem.zip
Any help is appreciated!
Andreas Weichelt
2011-08-24 13:02:39 UTC
Permalink
Hi,

I am facing a very similar problem. Did you find a solution (or at least workaround) in between?

Any reply is appreciated!
Post by Georg Steigmeier
Hi
I have a strange memory leak in my application running under CE 6.0. My
investigations showed that it has something to do with ActiveX controls and
starting/stopping threads.
Now I made a test application that has the same problem. It's a simple
dialog based application that uses MFC and has an ActiveX control on it
(application and ActiveX generated with the wizard). The application and the
ActiveX use MFC in a shared DLL. Now if a start a worker thread e.g. in an
event handler inside the dialog class, then I see that the available memory
shrinks (about 1600 bytes) after the thread stops. It doesn' matter if I use
CreateThread or AfxBeginThread to start the thread. Note that I don' do
anything inside the thread function, it just returns.
I don't have this problem if I remove the (dummy) ActiveX from the dialog.
There is also no leak if the ActiveX uses MFC in a static library.
Is there a problem inside MFC?
Post by Georg Steigmeier
By the way, I am using Visual Studio 2005 and therefore MFC 8.0.
Post by Anthony Wieser
Are you cleaning up the thread handle?
Anthony Wieser
Wieser Software Ltd
Post by Georg Steigmeier
Hi Anthony
Yes, the thread handles are closed. Another thing is, that the memory leak
only occurs under windows CE and not under Win32. I think it is something
inside MFC.
Post by Bill T
We had very similar behavior with CE5.0, and that MSFT recommended to us to
preload the MFC dll "early in the WinMain. This did correct the memory leak
we were seeing.
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
{
....
LoadLibrary(_T("mfcce400.dll"));
...
}
Post by Chris Tacke, eMVP
You have the full source to MFC....
C:\Program Files\Microsoft Visual Studio 9.0\VC\ce\atlmfc\src\mfc
For '05 it was something very similar (don't have it installed to check).
--
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
Post by Georg Steigmeier
Hi Chris
I find it quite difficult to debug the MFC internals. I tried to find
different behaviours between Win32 and WinCE, because the memory leak does
If my “worker” thread (created with CreateThread) ends, then I see that the
function CThreadSlotData::DeleteValues(CThreadData* pData, HINSTANCE hInst)
[afxtls.cpp] is called from the MFC DLL and from my ActiceX control.
- Under Win32 the DeleteValues() function is first called from my ActiveX
and here no delete operation really takes place, then it is called from the
MFC DLL and the thread data gets deleted.
- Under WinCE the DeleteValues() function is called in different order.
First from the MFC DLL and the thread data gets already deleted, then it is
called from my ActiveX and here no delete operation takes place anymore. But
in between it seems that new thread data is created through calls to
CThreadSlotData::SetValue(int nSlot, void* pValue).
Do you know if the behaviour under WinCE is wrong?
Thanks in advance
Georg
Post by Georg Steigmeier
I also tried it with Visual Studio 2008 and therefore MFC 9.0. The memory
leak problem still exists.
Post by Georg Steigmeier
http://cid-1e808bf3e2edf989.skydrive.live.com/self.aspx/.Public/MemLeakThreadProblem.zip
Any help is appreciated!
Post by Helge Kruse
Is there any other place where you can store the ZIP file. I found no way
to open it.
Helge
Continue reading on narkive:
Loading...