From: r norman on
On Tue, 10 Jul 2007 13:45:46 -0700, "Michael K. O'Neill"
<MikeAThon2000(a)nospam.hotmail.com> wrote:

My sincere thanks to your knowledge of this problem. I haven't
enabled themes (to my knowledge) on my system, but this may do it. If
it does, it saves me a lot of work. If it doesn't, at least I know
that the problem isn't me doing stupid things. Or at least, part of
the problem isn't; no doubt there are more stupid things that are
really my fault still waiting to bite me.

>
>"r norman" <r_s_norman@_comcast.net> wrote in message
>news:2895939efidggi556s7fbje0euhm2jd2d0(a)4ax.com...
>> I have traced a memory leak problem to CAsyncSocket::Create(). Is
>> this a known problem? Is there a workaround/solution/fix? Here is
>> sample code:
>>
>> for (int i=0; i<m_nReopenCount; ++i) {
>> CAsyncSocket *pAS = new CAsyncSocket;
>> pAS->Create();
>> pAS->Close();
>> delete pAS;
>> }
>>
>> Running this 1000 times uses up 1200 KBytes of memory, or just over 1
>> KByte per call. Commenting out the Create() leaves memory clean. (And
>> please don't complain about my bracketing style -- I like it.)
>>
>
>
>This is a known problem, acknowledged by Microsoft.
>
>Apparently, when using CAsyncSocket in a GUI app that also has XP visual
>styles (themes), there is a memory leak. The problem seems to be
>OS-dependent, and it might exist only under XP (running themes). It did not
>manifest itself under Win 2000, and it might be gone now from Vista.
>
>For one report, see "CSocket Consuming Memory Uncontrollably" at
>http://www.codeguru.com/forum/showthread.php?t=353944 . I am fairly certain
>that this is the same problem that you are seeing, since this report also
>includes the exact same number for the amount of leaked memory (i.e., 1200
>bytes per call to Create()).
>
>There is a parallel posting in the newsgroups at "HELP!! CSocket and
>CAsyncSocket Consumes Memory Uncontrollably" in the
>microsoft.public.win32.programmer.networks newsgroup, at
>http://groups.google.com/group/microsoft.public.win32.programmer.networks/browse_frm/thread/8490cdb5c4f18c76/b87332dbee0cb54c?tvc=1
>
>The "solution", if you can call it that, is to disable themes. See
>"CAsynCSocket Memory Leak Fix" at
>http://www.codeguru.com/forum/showthread.php?t=370761
>
>Mike
>
From: r norman on
On Tue, 10 Jul 2007 18:26:06 -0400, r norman <r_s_norman@_comcast.net>
wrote:

That did the trick! Many, many thanks, Michael. I don't understand
it, but then if it works, I don't care. The first CAsyncSocket uses a
bunch of memory no doubt because that is when all the socket code gets
loaded, as Joe pointed out. From then on it is all 0 bytes per
instance!

And many thanks again both to you Joe and you AliR for your major
assistance in getting to the bottom of this.

The problem, as Joe points out, is how such a serious problem can go
unfixed and unremarked. I won't comment on the corporation that is
responsible because I use and depend on so much of their products and
have done so since CP/M MASM and LINK. But the many people who blow
a gasket at the mere hint of that corporate name do have at least some
justification.

>On Tue, 10 Jul 2007 13:45:46 -0700, "Michael K. O'Neill"
><MikeAThon2000(a)nospam.hotmail.com> wrote:
>
>My sincere thanks to your knowledge of this problem. I haven't
>enabled themes (to my knowledge) on my system, but this may do it. If
>it does, it saves me a lot of work. If it doesn't, at least I know
>that the problem isn't me doing stupid things. Or at least, part of
>the problem isn't; no doubt there are more stupid things that are
>really my fault still waiting to bite me.
>
>>
>>"r norman" <r_s_norman@_comcast.net> wrote in message
>>news:2895939efidggi556s7fbje0euhm2jd2d0(a)4ax.com...
>>> I have traced a memory leak problem to CAsyncSocket::Create(). Is
>>> this a known problem? Is there a workaround/solution/fix? Here is
>>> sample code:
>>>
>>> for (int i=0; i<m_nReopenCount; ++i) {
>>> CAsyncSocket *pAS = new CAsyncSocket;
>>> pAS->Create();
>>> pAS->Close();
>>> delete pAS;
>>> }
>>>
>>> Running this 1000 times uses up 1200 KBytes of memory, or just over 1
>>> KByte per call. Commenting out the Create() leaves memory clean. (And
>>> please don't complain about my bracketing style -- I like it.)
>>>
>>
>>
>>This is a known problem, acknowledged by Microsoft.
>>
>>Apparently, when using CAsyncSocket in a GUI app that also has XP visual
>>styles (themes), there is a memory leak. The problem seems to be
>>OS-dependent, and it might exist only under XP (running themes). It did not
>>manifest itself under Win 2000, and it might be gone now from Vista.
>>
>>For one report, see "CSocket Consuming Memory Uncontrollably" at
>>http://www.codeguru.com/forum/showthread.php?t=353944 . I am fairly certain
>>that this is the same problem that you are seeing, since this report also
>>includes the exact same number for the amount of leaked memory (i.e., 1200
>>bytes per call to Create()).
>>
>>There is a parallel posting in the newsgroups at "HELP!! CSocket and
>>CAsyncSocket Consumes Memory Uncontrollably" in the
>>microsoft.public.win32.programmer.networks newsgroup, at
>>http://groups.google.com/group/microsoft.public.win32.programmer.networks/browse_frm/thread/8490cdb5c4f18c76/b87332dbee0cb54c?tvc=1
>>
>>The "solution", if you can call it that, is to disable themes. See
>>"CAsynCSocket Memory Leak Fix" at
>>http://www.codeguru.com/forum/showthread.php?t=370761
>>
>>Mike
>>
From: Doug Harrison [MVP] on
On Tue, 10 Jul 2007 13:45:46 -0700, "Michael K. O'Neill"
<MikeAThon2000(a)nospam.hotmail.com> wrote:

>"r norman" <r_s_norman@_comcast.net> wrote in message
>news:2895939efidggi556s7fbje0euhm2jd2d0(a)4ax.com...
>> I have traced a memory leak problem to CAsyncSocket::Create(). Is
>> this a known problem? Is there a workaround/solution/fix? Here is
>> sample code:
>>
>> for (int i=0; i<m_nReopenCount; ++i) {
>> CAsyncSocket *pAS = new CAsyncSocket;
>> pAS->Create();
>> pAS->Close();
>> delete pAS;
>> }
>>
>> Running this 1000 times uses up 1200 KBytes of memory, or just over 1
>> KByte per call. Commenting out the Create() leaves memory clean. (And
>> please don't complain about my bracketing style -- I like it.)
>>
>
>This is a known problem, acknowledged by Microsoft.
>
>Apparently, when using CAsyncSocket in a GUI app that also has XP visual
>styles (themes), there is a memory leak. The problem seems to be
>OS-dependent, and it might exist only under XP (running themes). It did not
>manifest itself under Win 2000, and it might be gone now from Vista.
>
>For one report, see "CSocket Consuming Memory Uncontrollably" at
>http://www.codeguru.com/forum/showthread.php?t=353944 . I am fairly certain
>that this is the same problem that you are seeing, since this report also
>includes the exact same number for the amount of leaked memory (i.e., 1200
>bytes per call to Create()).
>
>There is a parallel posting in the newsgroups at "HELP!! CSocket and
>CAsyncSocket Consumes Memory Uncontrollably" in the
>microsoft.public.win32.programmer.networks newsgroup, at
>http://groups.google.com/group/microsoft.public.win32.programmer.networks/browse_frm/thread/8490cdb5c4f18c76/b87332dbee0cb54c?tvc=1
>
>The "solution", if you can call it that, is to disable themes. See
>"CAsynCSocket Memory Leak Fix" at
>http://www.codeguru.com/forum/showthread.php?t=370761

Interesting! The problem appears to be the creation of a top-level,
non-child window by CAsyncSocket::AttachHandle. For example, if I replace
norman's loop body with the following, I observe the same "leak" in Process
Explorer:

CWnd* p = new CSocketWnd;
#if 1
p->CreateEx(0, AfxRegisterWndClass(0), 0,
WS_OVERLAPPED, CRect(), 0, 0);
#else
p->Create(AfxRegisterWndClass(0), 0,
WS_CHILD, CRect(), AfxGetMainWnd(), 0);
#endif
p->DestroyWindow();
delete p;

If I use #if 0, a child window is created instead, and I don't observe the
leak.

--
Doug Harrison
Visual C++ MVP
From: Michael K. O'Neill on

"Doug Harrison [MVP]" <dsh(a)mvps.org> wrote in message
news:kad893pun39rv6hlti7dt50kgu2j43kue2(a)4ax.com...
> On Tue, 10 Jul 2007 13:45:46 -0700, "Michael K. O'Neill"
> <MikeAThon2000(a)nospam.hotmail.com> wrote:
>
> >"r norman" <r_s_norman@_comcast.net> wrote in message
> >news:2895939efidggi556s7fbje0euhm2jd2d0(a)4ax.com...
> >> I have traced a memory leak problem to CAsyncSocket::Create(). Is
> >> this a known problem? Is there a workaround/solution/fix? Here is
> >> sample code:
> >>
> >> for (int i=0; i<m_nReopenCount; ++i) {
> >> CAsyncSocket *pAS = new CAsyncSocket;
> >> pAS->Create();
> >> pAS->Close();
> >> delete pAS;
> >> }
> >>
> >> Running this 1000 times uses up 1200 KBytes of memory, or just over 1
> >> KByte per call. Commenting out the Create() leaves memory clean. (And
> >> please don't complain about my bracketing style -- I like it.)
> >>
> >
> >This is a known problem, acknowledged by Microsoft.
> >
> >Apparently, when using CAsyncSocket in a GUI app that also has XP visual
> >styles (themes), there is a memory leak. The problem seems to be
> >OS-dependent, and it might exist only under XP (running themes). It did
not
> >manifest itself under Win 2000, and it might be gone now from Vista.
> >
> >For one report, see "CSocket Consuming Memory Uncontrollably" at
> >http://www.codeguru.com/forum/showthread.php?t=353944 . I am fairly
certain
> >that this is the same problem that you are seeing, since this report also
> >includes the exact same number for the amount of leaked memory (i.e.,
1200
> >bytes per call to Create()).
> >
> >There is a parallel posting in the newsgroups at "HELP!! CSocket and
> >CAsyncSocket Consumes Memory Uncontrollably" in the
> >microsoft.public.win32.programmer.networks newsgroup, at
>
>http://groups.google.com/group/microsoft.public.win32.programmer.networks/b
rowse_frm/thread/8490cdb5c4f18c76/b87332dbee0cb54c?tvc=1
> >
> >The "solution", if you can call it that, is to disable themes. See
> >"CAsynCSocket Memory Leak Fix" at
> >http://www.codeguru.com/forum/showthread.php?t=370761
>
> Interesting! The problem appears to be the creation of a top-level,
> non-child window by CAsyncSocket::AttachHandle. For example, if I replace
> norman's loop body with the following, I observe the same "leak" in
Process
> Explorer:
>
> CWnd* p = new CSocketWnd;
> #if 1
> p->CreateEx(0, AfxRegisterWndClass(0), 0,
> WS_OVERLAPPED, CRect(), 0, 0);
> #else
> p->Create(AfxRegisterWndClass(0), 0,
> WS_CHILD, CRect(), AfxGetMainWnd(), 0);
> #endif
> p->DestroyWindow();
> delete p;
>
> If I use #if 0, a child window is created instead, and I don't observe the
> leak.
>
> --
> Doug Harrison
> Visual C++ MVP

That *is* very interesting. When we looked at this two years ago, we
noticed that the leak disappeared when creating a child window, but we
didn't look any furhter, and we didn't think to try a top-level window. We
eventually concluded that the problem was related to the creation of the
socket's hidden window, but that it didn't show itself unless XP visual
styles (themes) was enabled, i.e., inclusion of a manifest with the program.

With a top-level window, does it make a difference as to whether themes is
or is not enabled? Was our answer back then completely off-base?

Mike


From: Doug Harrison [MVP] on
On Tue, 10 Jul 2007 21:19:23 -0700, "Michael K. O'Neill"
<mikeathon2000(a)nospam.hotmail.com> wrote:

>That *is* very interesting. When we looked at this two years ago, we
>noticed that the leak disappeared when creating a child window, but we
>didn't look any furhter, and we didn't think to try a top-level window. We
>eventually concluded that the problem was related to the creation of the
>socket's hidden window, but that it didn't show itself unless XP visual
>styles (themes) was enabled, i.e., inclusion of a manifest with the program.
>
>With a top-level window, does it make a difference as to whether themes is
>or is not enabled? Was our answer back then completely off-base?

I haven't tested the theme aspect, but assuming it's correct, there's
clearly a bad interaction between top-level windows and themes that has
nothing to do with sockets. The CreateEx call is a simplified version of
what CAsyncSocket::AttachHandle does. You can easily test this. Just create
a default MFC SDI app and put the loop in OnAppAbout; at least, that's my
favorite way to test stuff like this. So I had:

void Cdef_sockApp::OnAppAbout()
{
for (int i=0; i<1000; ++i)
{
#if 0
CAsyncSocket *pAS = new CAsyncSocket;
pAS->Create();
pAS->Close();
delete pAS;
#else
CWnd* p = new CWnd;
#if 1
p->CreateEx(0, AfxRegisterWndClass(0), 0,
WS_OVERLAPPED, CRect(), 0, 0);
#else
p->Create(AfxRegisterWndClass(0), 0,
WS_CHILD, CRect(), AfxGetMainWnd(), 0);
#endif
p->DestroyWindow();
delete p;
#endif
}

CAboutDlg aboutDlg;
aboutDlg.DoModal();
}

The next step would be to reduce this to the Windows API level and do it
all in terms of raw HWNDs. The window and window class styles might also be
a factor.

--
Doug Harrison
Visual C++ MVP