From: Joseph M. Newcomer on
See below...
On Tue, 6 May 2008 06:01:01 -0700, App shows memory leak on some machines.
<Appshowsmemoryleakonsomemachines(a)discussions.microsoft.com> wrote:

>
>Thank you Joseph for your reply.
>
>I've used AfxBeginThread() function to launch thread and subsequently
>AfxEndThread() to end it.
****
This is a mistake. There is exactly ONE reliable and correct way to end a thread, and
that is to return from the top-level thread function. If you call AfxEndThread, you will
leak storage, because every object on the stack that has allocated memory will now fail to
deallocate. Remove every instance of AfxEndThread. Recode as necessary. Note that
throwing an exception which you catch in the top-level thread function is a possible
solution. But as soon as you write a thread-termination function in a thread, your first
reaction should be "This is SO wrong!"
****
>I tried using CloseHandle() and checked its return value, also checked
>return value for GetLastError(), to make sure that handle is getting closed.
>Return values were as expected. But, it made no difference in behaviour of
>exe.
>
>This part of code looks like
>//--------------------------------
>CWinThread* ThPtr = AfxBeginThread(..);
>bool b = ::CloseHandle(ThPtr->m_hThread);
****
No, this would be an error. You should not close the thread handle until the thread has
completed. Note that a thread will automatically delete the CWinTHread object when it
completes, and this closes the handle
****
>int n = GetLastError();
****
GetLastError is completely meaningless (with a very tiny number of exceptions) unless you
know that the last operation failed.
****
>
>thread function:
>ThreadFunc()
>{
>.
>.
>.
>AfxEndThread();
****
Since this is in your top-level thread function, just delete it. Assume that writing
AfxEndThread on your own is ALWAYS a programming error.
****
>returb bRet;
>}
>//--------------------------------
>
>I've used socket and DB calls inside thread function. For DB communication,
>I've used 'msado15.dll'.
>
>I'll like to know, in which cases application will behave differently (in
>terms of memory leaks) on different machine?
****
Perhaps different versions of the msado15.dll? I have no idea. But something that leaks
memory on one machine but not on another is indicative of a difference in the operating
environments of the two machines, and you would have to determine exactly what components
differed between the two machines.
joe
****
>
>Thanks,
>Digvijay
>
>"Joseph M. Newcomer" wrote:
>
>> It is FAR more likely that what happened is that you are failing to close the thread
>> handle of the thread, resulting in a leakage of thread stacks. Are you certain every
>> thread handle is being closed?
>>
>> The task manager may suggest that memory usage is increasing (it is one of the few valid
>> reports about memory it is actually capable of), but you need to rule out lots of other
>> explanations before pointing to MDAC (not that it might not be the cause, but the most
>> common error I've found in multithreaded leaks is stack leakage). I would suggest
>> downloading the free App Verifier from the Microsoft site and turning on all memory
>> management options.
>> joe
>>
>> On Fri, 2 May 2008 07:48:02 -0700, Prashant <Prashant(a)discussions.microsoft.com> wrote:
>>
>> >The application is a multithreaded exe involving socket communication. The
>> >memory usage in task manager increases when exe is running. It never comes
>> >down. In other words, for each thread it increases and never comes down even
>> >after that thread dies.
>> >
>> >Thanks,
>> >Digvijay
>> >
>> >"Joseph M. Newcomer" wrote:
>> >
>> >> How do you detect that there is a memory leak? You assert this is happening without
>> >> explaining why you think it is so.
>> >> joe
>> >> On Thu, 1 May 2008 05:11:01 -0700, App shows memory leak on some machines. <App shows
>> >> memory leak on some machines.(a)discussions.microsoft.com> wrote:
>> >>
>> >> >Hi,
>> >> >
>> >> >My application (developed in VC++) is running fine on most of the
>> >> >machines(without any memory leak). But, it is showing memory leaks on
>> >> >production machine. Even on development side it is showing memory leaks on
>> >> >few machine. Why it is so?
>> >> >
>> >> >Hardware configuration of these machines are different. But, I think this
>> >> >difference should not create any memory leak in application.
>> >> >
>> >> >My application is using database communication and socket communication APIs.
>> >> >
>> >> >Is there any relation bet Hardware configuration and MDAC version?????
>> >> >Is MDAC 2.82.3959.0 having memory leak problems??
>> >> >
>> >> >I've checked my code several times with different APIs and tools, but it has
>> >> >no memory leak. Please help me solving the problem. I've tried to solve
>> >> >problem in many ways for a long time, but not getting any output.
>> >> >
>> >> >MDAC versions on these machines are mentioned below.
>> >> >Development machine : 2.81.1128.0
>> >> >Production machines : 2.82.3959.0
>> >> >
>> >> >OS : XP SP2, Windows 2003 server
>> >> Joseph M. Newcomer [MVP]
>> >> email: newcomer(a)flounder.com
>> >> Web: http://www.flounder.com
>> >> MVP Tips: http://www.flounder.com/mvp_tips.htm
>> >>
>> Joseph M. Newcomer [MVP]
>> email: newcomer(a)flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Nick Schultz on
Use Process Explorer from www.sysinternals.com . It is free and is MUCH
better than Task Manager, you can double click on a process and get very
detailed information, including a list of threads and memory usage. It also
shows processes in a tree format rather than just a flat list...overall its
a better tool.

Go to the Options menu and select "Replace Task Manager" and Process
Explorer will pop up whenever you press CTRL+ALT+DEL.

Nick


"App shows memory leak on some machines."
<Appshowsmemoryleakonsomemachines(a)discussions.microsoft.com> wrote in
message news:91870733-F651-4E90-A5E7-88CB6170E89D(a)microsoft.com...
>
> Thank you Joseph for your reply.
>
> I've used AfxBeginThread() function to launch thread and subsequently
> AfxEndThread() to end it.
> I tried using CloseHandle() and checked its return value, also checked
> return value for GetLastError(), to make sure that handle is getting
> closed.
> Return values were as expected. But, it made no difference in behaviour of
> exe.
>
> This part of code looks like
> //--------------------------------
> CWinThread* ThPtr = AfxBeginThread(..);
> bool b = ::CloseHandle(ThPtr->m_hThread);
> int n = GetLastError();
>
> thread function:
> ThreadFunc()
> {
> .
> .
> .
> AfxEndThread();
> returb bRet;
> }
> //--------------------------------
>
> I've used socket and DB calls inside thread function. For DB
> communication,
> I've used 'msado15.dll'.
>
> I'll like to know, in which cases application will behave differently (in
> terms of memory leaks) on different machine?
>
> Thanks,
> Digvijay
>
> "Joseph M. Newcomer" wrote:
>
>> It is FAR more likely that what happened is that you are failing to close
>> the thread
>> handle of the thread, resulting in a leakage of thread stacks. Are you
>> certain every
>> thread handle is being closed?
>>
>> The task manager may suggest that memory usage is increasing (it is one
>> of the few valid
>> reports about memory it is actually capable of), but you need to rule out
>> lots of other
>> explanations before pointing to MDAC (not that it might not be the cause,
>> but the most
>> common error I've found in multithreaded leaks is stack leakage). I
>> would suggest
>> downloading the free App Verifier from the Microsoft site and turning on
>> all memory
>> management options.
>> joe
>>
>> On Fri, 2 May 2008 07:48:02 -0700, Prashant
>> <Prashant(a)discussions.microsoft.com> wrote:
>>
>> >The application is a multithreaded exe involving socket communication.
>> >The
>> >memory usage in task manager increases when exe is running. It never
>> >comes
>> >down. In other words, for each thread it increases and never comes down
>> >even
>> >after that thread dies.
>> >
>> >Thanks,
>> >Digvijay
>> >
>> >"Joseph M. Newcomer" wrote:
>> >
>> >> How do you detect that there is a memory leak? You assert this is
>> >> happening without
>> >> explaining why you think it is so.
>> >> joe
>> >> On Thu, 1 May 2008 05:11:01 -0700, App shows memory leak on some
>> >> machines. <App shows
>> >> memory leak on some machines.(a)discussions.microsoft.com> wrote:
>> >>
>> >> >Hi,
>> >> >
>> >> >My application (developed in VC++) is running fine on most of the
>> >> >machines(without any memory leak). But, it is showing memory leaks on
>> >> >production machine. Even on development side it is showing memory
>> >> >leaks on
>> >> >few machine. Why it is so?
>> >> >
>> >> >Hardware configuration of these machines are different. But, I think
>> >> >this
>> >> >difference should not create any memory leak in application.
>> >> >
>> >> >My application is using database communication and socket
>> >> >communication APIs.
>> >> >
>> >> >Is there any relation bet Hardware configuration and MDAC
>> >> >version?????
>> >> >Is MDAC 2.82.3959.0 having memory leak problems??
>> >> >
>> >> >I've checked my code several times with different APIs and tools, but
>> >> >it has
>> >> >no memory leak. Please help me solving the problem. I've tried to
>> >> >solve
>> >> >problem in many ways for a long time, but not getting any output.
>> >> >
>> >> >MDAC versions on these machines are mentioned below.
>> >> >Development machine : 2.81.1128.0
>> >> >Production machines : 2.82.3959.0
>> >> >
>> >> >OS : XP SP2, Windows 2003 server
>> >> Joseph M. Newcomer [MVP]
>> >> email: newcomer(a)flounder.com
>> >> Web: http://www.flounder.com
>> >> MVP Tips: http://www.flounder.com/mvp_tips.htm
>> >>
>> Joseph M. Newcomer [MVP]
>> email: newcomer(a)flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>>


From: App shows memory leak on some machines. on

Actually I've originally used AfxBeginThread() function to launch thread and
just a return statement while exiting thread. And since I was getting handle
leak along with memory leak, I tried using CloseHandle() in the previously
mentioned way. After this change I found no difference in app's behavior.
I again revert back to original code where I've used a return statement in
the top level thread function. Still I can find same behavior (handle +
memory leak).


I'll like to add some more points here-

-I am launching only one thread from main thread.
Lets say ThreadFirst is launched by main thread. Now, all other threads are
getting launched by ThreadFirst.
It's required since main thread needs to do other task.

-Again there is a critical section (semaphore) in thread function. Many
threads are keep on hanging at the start of critical section waiting for
others threads to leave.

Can it create any problem??

Thanks,
Digvijay

"Nick Schultz" wrote:

> Use Process Explorer from www.sysinternals.com . It is free and is MUCH
> better than Task Manager, you can double click on a process and get very
> detailed information, including a list of threads and memory usage. It also
> shows processes in a tree format rather than just a flat list...overall its
> a better tool.
>
> Go to the Options menu and select "Replace Task Manager" and Process
> Explorer will pop up whenever you press CTRL+ALT+DEL.
>
> Nick
>
>
> "App shows memory leak on some machines."
> <Appshowsmemoryleakonsomemachines(a)discussions.microsoft.com> wrote in
> message news:91870733-F651-4E90-A5E7-88CB6170E89D(a)microsoft.com...
> >
> > Thank you Joseph for your reply.
> >
> > I've used AfxBeginThread() function to launch thread and subsequently
> > AfxEndThread() to end it.
> > I tried using CloseHandle() and checked its return value, also checked
> > return value for GetLastError(), to make sure that handle is getting
> > closed.
> > Return values were as expected. But, it made no difference in behaviour of
> > exe.
> >
> > This part of code looks like
> > //--------------------------------
> > CWinThread* ThPtr = AfxBeginThread(..);
> > bool b = ::CloseHandle(ThPtr->m_hThread);
> > int n = GetLastError();
> >
> > thread function:
> > ThreadFunc()
> > {
> > .
> > .
> > .
> > AfxEndThread();
> > returb bRet;
> > }
> > //--------------------------------
> >
> > I've used socket and DB calls inside thread function. For DB
> > communication,
> > I've used 'msado15.dll'.
> >
> > I'll like to know, in which cases application will behave differently (in
> > terms of memory leaks) on different machine?
> >
> > Thanks,
> > Digvijay
> >
> > "Joseph M. Newcomer" wrote:
> >
> >> It is FAR more likely that what happened is that you are failing to close
> >> the thread
> >> handle of the thread, resulting in a leakage of thread stacks. Are you
> >> certain every
> >> thread handle is being closed?
> >>
> >> The task manager may suggest that memory usage is increasing (it is one
> >> of the few valid
> >> reports about memory it is actually capable of), but you need to rule out
> >> lots of other
> >> explanations before pointing to MDAC (not that it might not be the cause,
> >> but the most
> >> common error I've found in multithreaded leaks is stack leakage). I
> >> would suggest
> >> downloading the free App Verifier from the Microsoft site and turning on
> >> all memory
> >> management options.
> >> joe
> >>
> >> On Fri, 2 May 2008 07:48:02 -0700, Prashant
> >> <Prashant(a)discussions.microsoft.com> wrote:
> >>
> >> >The application is a multithreaded exe involving socket communication.
> >> >The
> >> >memory usage in task manager increases when exe is running. It never
> >> >comes
> >> >down. In other words, for each thread it increases and never comes down
> >> >even
> >> >after that thread dies.
> >> >
> >> >Thanks,
> >> >Digvijay
> >> >
> >> >"Joseph M. Newcomer" wrote:
> >> >
> >> >> How do you detect that there is a memory leak? You assert this is
> >> >> happening without
> >> >> explaining why you think it is so.
> >> >> joe
> >> >> On Thu, 1 May 2008 05:11:01 -0700, App shows memory leak on some
> >> >> machines. <App shows
> >> >> memory leak on some machines.(a)discussions.microsoft.com> wrote:
> >> >>
> >> >> >Hi,
> >> >> >
> >> >> >My application (developed in VC++) is running fine on most of the
> >> >> >machines(without any memory leak). But, it is showing memory leaks on
> >> >> >production machine. Even on development side it is showing memory
> >> >> >leaks on
> >> >> >few machine. Why it is so?
> >> >> >
> >> >> >Hardware configuration of these machines are different. But, I think
> >> >> >this
> >> >> >difference should not create any memory leak in application.
> >> >> >
> >> >> >My application is using database communication and socket
> >> >> >communication APIs.
> >> >> >
> >> >> >Is there any relation bet Hardware configuration and MDAC
> >> >> >version?????
> >> >> >Is MDAC 2.82.3959.0 having memory leak problems??
> >> >> >
> >> >> >I've checked my code several times with different APIs and tools, but
> >> >> >it has
> >> >> >no memory leak. Please help me solving the problem. I've tried to
> >> >> >solve
> >> >> >problem in many ways for a long time, but not getting any output.
> >> >> >
> >> >> >MDAC versions on these machines are mentioned below.
> >> >> >Development machine : 2.81.1128.0
> >> >> >Production machines : 2.82.3959.0
> >> >> >
> >> >> >OS : XP SP2, Windows 2003 server
> >> >> Joseph M. Newcomer [MVP]
> >> >> email: newcomer(a)flounder.com
> >> >> Web: http://www.flounder.com
> >> >> MVP Tips: http://www.flounder.com/mvp_tips.htm
> >> >>
> >> Joseph M. Newcomer [MVP]
> >> email: newcomer(a)flounder.com
> >> Web: http://www.flounder.com
> >> MVP Tips: http://www.flounder.com/mvp_tips.htm
> >>
>
>
>
From: Scott McPhillips [MVP] on
"App shows memory leak on some machines."
<Appshowsmemoryleakonsomemachines(a)discussions.microsoft.com> wrote in
message news:8B3AB5F5-5DE2-4B17-9F87-99625735C880(a)microsoft.com...
> -Again there is a critical section (semaphore) in thread function. Many
> threads are keep on hanging at the start of critical section waiting for
> others threads to leave.
>
> Can it create any problem??


That is exactly what critical section is supposed to do, so only one thread
at a time will access shared data. It solves the data synchronization
problem. If it causes poor performance for your threads then you can look
for design changes that reduce the number of accesses to shared data.

--
Scott McPhillips [VC++ MVP]

From: Joseph M. Newcomer on
See below...
On Wed, 7 May 2008 02:11:00 -0700, App shows memory leak on some machines.
<Appshowsmemoryleakonsomemachines(a)discussions.microsoft.com> wrote:

>
>Actually I've originally used AfxBeginThread() function to launch thread and
>just a return statement while exiting thread. And since I was getting handle
>leak along with memory leak, I tried using CloseHandle() in the previously
>mentioned way. After this change I found no difference in app's behavior.
>I again revert back to original code where I've used a return statement in
>the top level thread function. Still I can find same behavior (handle +
>memory leak).
>
>
>I'll like to add some more points here-
>
>-I am launching only one thread from main thread.
>Lets say ThreadFirst is launched by main thread. Now, all other threads are
>getting launched by ThreadFirst.
>It's required since main thread needs to do other task.
>
>-Again there is a critical section (semaphore) in thread function. Many
>threads are keep on hanging at the start of critical section waiting for
>others threads to leave.
****
I suspect you are confused. A CRITICAL_SECTION is a very efficient low-level
synchronization primitive. A mutex is a considerably less efficient synchronization
primitive. A semaphore is something completely different, and fulfills the purposes of a
semaphore (a counted exclusion object) and has little to do with mutual exclusion of
multiple threads. So when you say "critical section (semaphore)" I become suspicious,
because semaphores would not be appropriate here.

And yes, if you have a synchronization object, and you have high contention, you are
likely to have a lot of threads hanging.

Some rules:
Don't use synchronization. Design your code so there is no need for
concurrent access
Lock data, not code
Lock the smallest possible amount of data for the shortest possible time

In the absence of any code example showing the locking, it is hard to guess what you might
have done, but the most likely cause is that you have violated one or more of the above
rules.

Locking is done when threads rub together. That means there is friction. And just like
in mechanical systems, friction generates heat and wastes energy. I tend to view
synchronization as something that should be avoided by making sure threads are not
actually rubbing. See my essay "The Best Synchronization is No Synchronization" on my MVP
Tips site. Every once in a while, you need concurrent access, but the more I work with
threads, the less I do it.
joe
****
>
>Can it create any problem??
>
>Thanks,
>Digvijay
>
>"Nick Schultz" wrote:
>
>> Use Process Explorer from www.sysinternals.com . It is free and is MUCH
>> better than Task Manager, you can double click on a process and get very
>> detailed information, including a list of threads and memory usage. It also
>> shows processes in a tree format rather than just a flat list...overall its
>> a better tool.
>>
>> Go to the Options menu and select "Replace Task Manager" and Process
>> Explorer will pop up whenever you press CTRL+ALT+DEL.
>>
>> Nick
>>
>>
>> "App shows memory leak on some machines."
>> <Appshowsmemoryleakonsomemachines(a)discussions.microsoft.com> wrote in
>> message news:91870733-F651-4E90-A5E7-88CB6170E89D(a)microsoft.com...
>> >
>> > Thank you Joseph for your reply.
>> >
>> > I've used AfxBeginThread() function to launch thread and subsequently
>> > AfxEndThread() to end it.
>> > I tried using CloseHandle() and checked its return value, also checked
>> > return value for GetLastError(), to make sure that handle is getting
>> > closed.
>> > Return values were as expected. But, it made no difference in behaviour of
>> > exe.
>> >
>> > This part of code looks like
>> > //--------------------------------
>> > CWinThread* ThPtr = AfxBeginThread(..);
>> > bool b = ::CloseHandle(ThPtr->m_hThread);
>> > int n = GetLastError();
>> >
>> > thread function:
>> > ThreadFunc()
>> > {
>> > .
>> > .
>> > .
>> > AfxEndThread();
>> > returb bRet;
>> > }
>> > //--------------------------------
>> >
>> > I've used socket and DB calls inside thread function. For DB
>> > communication,
>> > I've used 'msado15.dll'.
>> >
>> > I'll like to know, in which cases application will behave differently (in
>> > terms of memory leaks) on different machine?
>> >
>> > Thanks,
>> > Digvijay
>> >
>> > "Joseph M. Newcomer" wrote:
>> >
>> >> It is FAR more likely that what happened is that you are failing to close
>> >> the thread
>> >> handle of the thread, resulting in a leakage of thread stacks. Are you
>> >> certain every
>> >> thread handle is being closed?
>> >>
>> >> The task manager may suggest that memory usage is increasing (it is one
>> >> of the few valid
>> >> reports about memory it is actually capable of), but you need to rule out
>> >> lots of other
>> >> explanations before pointing to MDAC (not that it might not be the cause,
>> >> but the most
>> >> common error I've found in multithreaded leaks is stack leakage). I
>> >> would suggest
>> >> downloading the free App Verifier from the Microsoft site and turning on
>> >> all memory
>> >> management options.
>> >> joe
>> >>
>> >> On Fri, 2 May 2008 07:48:02 -0700, Prashant
>> >> <Prashant(a)discussions.microsoft.com> wrote:
>> >>
>> >> >The application is a multithreaded exe involving socket communication.
>> >> >The
>> >> >memory usage in task manager increases when exe is running. It never
>> >> >comes
>> >> >down. In other words, for each thread it increases and never comes down
>> >> >even
>> >> >after that thread dies.
>> >> >
>> >> >Thanks,
>> >> >Digvijay
>> >> >
>> >> >"Joseph M. Newcomer" wrote:
>> >> >
>> >> >> How do you detect that there is a memory leak? You assert this is
>> >> >> happening without
>> >> >> explaining why you think it is so.
>> >> >> joe
>> >> >> On Thu, 1 May 2008 05:11:01 -0700, App shows memory leak on some
>> >> >> machines. <App shows
>> >> >> memory leak on some machines.(a)discussions.microsoft.com> wrote:
>> >> >>
>> >> >> >Hi,
>> >> >> >
>> >> >> >My application (developed in VC++) is running fine on most of the
>> >> >> >machines(without any memory leak). But, it is showing memory leaks on
>> >> >> >production machine. Even on development side it is showing memory
>> >> >> >leaks on
>> >> >> >few machine. Why it is so?
>> >> >> >
>> >> >> >Hardware configuration of these machines are different. But, I think
>> >> >> >this
>> >> >> >difference should not create any memory leak in application.
>> >> >> >
>> >> >> >My application is using database communication and socket
>> >> >> >communication APIs.
>> >> >> >
>> >> >> >Is there any relation bet Hardware configuration and MDAC
>> >> >> >version?????
>> >> >> >Is MDAC 2.82.3959.0 having memory leak problems??
>> >> >> >
>> >> >> >I've checked my code several times with different APIs and tools, but
>> >> >> >it has
>> >> >> >no memory leak. Please help me solving the problem. I've tried to
>> >> >> >solve
>> >> >> >problem in many ways for a long time, but not getting any output.
>> >> >> >
>> >> >> >MDAC versions on these machines are mentioned below.
>> >> >> >Development machine : 2.81.1128.0
>> >> >> >Production machines : 2.82.3959.0
>> >> >> >
>> >> >> >OS : XP SP2, Windows 2003 server
>> >> >> Joseph M. Newcomer [MVP]
>> >> >> email: newcomer(a)flounder.com
>> >> >> Web: http://www.flounder.com
>> >> >> MVP Tips: http://www.flounder.com/mvp_tips.htm
>> >> >>
>> >> Joseph M. Newcomer [MVP]
>> >> email: newcomer(a)flounder.com
>> >> Web: http://www.flounder.com
>> >> MVP Tips: http://www.flounder.com/mvp_tips.htm
>> >>
>>
>>
>>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm