Memory leak after closing thread.

M

Michal

Before I explain exactly where is the problem I will show my code:
(Platform - Windows CE 5, VisualStudio 2005)

The interesting part of source code is below:

/**************************************************************/
class classA {
public:
static DWORD WINAPI testingThread(LPVOID lpvoid);
HANDLE m_hThread;
void startthread();

};

DWORD WINAPI classA::testingThread(LPVOID lpvoid)
{
return 1;

}

void classA::startthreadk()
{
for(int counter = 0 ; counter < 100 ; counter++) {
MEMORYSTATUS memInfo;
memInfo.dwLength = sizeof(memInfo);
GlobalMemoryStatus(&memInfo);
DWORD dwThreadId = 0;
m_hThread = CreateThread(NULL, 0, testingThread, this,
0,
&dwThreadId);
DWORD dwRet = WaitForSingleObject(m_hThread,
INFINITE);
if(WAIT_OBJECT_0 != dwRet) {
TRACE(_T("Something wrong !!! \n"));
}
int returnHandle = CloseHandle(m_hThread);
TRACE(_T("After closing handle = %d
\n"),returnHandle);
m_hThread = NULL;
MEMORYSTATUS memInfo1;
memInfo1.dwLength = sizeof(memInfo1);
GlobalMemoryStatus(&memInfo1);
TRACE(_T("%d Test %d\n"),counter, memInfo.dwAvailPhys
-
memInfo1.dwAvailPhys);
}

}

/**************************************************************/

The problem is that I don't know why but sometimes (usually one per
three times)
I can see the memory leak of 4096 bytes. Why? Do I do something wrong
with closing
thread? (Of course the program without creating thread works ok).
Closehandle
always returns 1, so everything should be ok.

I will be grateful for any response.

Michal
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Before I explain exactly where is the problem I will show my code:
(Platform - Windows CE 5, VisualStudio 2005)

The interesting part of source code is below:

/**************************************************************/
class classA {
public:
static DWORD WINAPI testingThread(LPVOID lpvoid);
HANDLE m_hThread;
void startthread();

};

DWORD WINAPI classA::testingThread(LPVOID lpvoid)
{
return 1;

}

void classA::startthreadk()
{
for(int counter = 0 ; counter < 100 ; counter++) {
MEMORYSTATUS memInfo;
memInfo.dwLength = sizeof(memInfo);
GlobalMemoryStatus(&memInfo);
DWORD dwThreadId = 0;
m_hThread = CreateThread(NULL, 0, testingThread, this,
0,
&dwThreadId);
DWORD dwRet = WaitForSingleObject(m_hThread,
INFINITE);
if(WAIT_OBJECT_0 != dwRet) {
TRACE(_T("Something wrong !!! \n"));
}
int returnHandle = CloseHandle(m_hThread);
TRACE(_T("After closing handle = %d
\n"),returnHandle);
m_hThread = NULL;
MEMORYSTATUS memInfo1;
memInfo1.dwLength = sizeof(memInfo1);
GlobalMemoryStatus(&memInfo1);
TRACE(_T("%d Test %d\n"),counter, memInfo.dwAvailPhys
-
memInfo1.dwAvailPhys);
}

}

/**************************************************************/

The problem is that I don't know why but sometimes (usually one per
three times)
I can see the memory leak of 4096 bytes. Why? Do I do something wrong
with closing
thread? (Of course the program without creating thread works ok).
Closehandle
always returns 1, so everything should be ok.

I will be grateful for any response.

Michal

You should ask in a Windows group instead.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAksWOxAACgkQG6NzcAXitM+HygCfZXq+CMdgqiriYLbPx11GuPTD
8owAnRE8LDVxOiN09e3EY15nO80KPVkd
=DNzD
-----END PGP SIGNATURE-----
 
M

Michael Doubez

Before I explain exactly where is the problem I will show my code:
(Platform - Windows CE 5, VisualStudio 2005)

The interesting part of source code is below:

/**************************************************************/
class classA {
        public:
        static DWORD WINAPI testingThread(LPVOID lpvoid);
        HANDLE m_hThread;
        void startthread();

};

DWORD WINAPI classA::testingThread(LPVOID lpvoid)
{
        return 1;

}

void classA::startthreadk()
{
        for(int counter = 0 ; counter < 100 ; counter++) {
                MEMORYSTATUS   memInfo;
                memInfo.dwLength = sizeof(memInfo);
                GlobalMemoryStatus(&memInfo);
                DWORD   dwThreadId = 0;
                m_hThread = CreateThread(NULL, 0, testingThread, this,
0,
&dwThreadId);
                DWORD dwRet = WaitForSingleObject(m_hThread,
INFINITE);
                if(WAIT_OBJECT_0 != dwRet) {
                        TRACE(_T("Something wrong !!! \n"));
                }
                int returnHandle = CloseHandle(m_hThread);
                TRACE(_T("After closing handle = %d
\n"),returnHandle);
                m_hThread = NULL;
                MEMORYSTATUS   memInfo1;
                memInfo1.dwLength = sizeof(memInfo1);
                GlobalMemoryStatus(&memInfo1);
                TRACE(_T("%d Test %d\n"),counter, memInfo..dwAvailPhys
-
memInfo1.dwAvailPhys);
        }

}

/**************************************************************/

The problem is that I don't know why but sometimes (usually one per
three times)
I can see the memory leak of 4096 bytes. Why? Do I do something wrong
with closing
thread? (Of course the program without creating thread works ok).
Closehandle
always returns 1, so everything should be ok.

The memeory is managed by your OS. It may not be returned to the
system immediately but kept a little time for future allocation (it
saves requests to the system).

That's what usually happen under Linux and I expect the same happens
in Windows.
 
J

James Kanze

Before I explain exactly where is the problem I will show my
code: (Platform - Windows CE 5, VisualStudio 2005)

[...]
The problem is that I don't know why but sometimes (usually
one per three times) I can see the memory leak of 4096 bytes.

How do you "see" a memory leak?
 
M

Michal

The memeory is managed by your OS. It may not be returned to the
system immediately but kept a little time for future allocation (it
saves requests to the system).

That's what usually happen under Linux and I expect the same happens
in Windows.

--
Hm... I'm not sure, but when I was waitnig for a few minutes... the
memory was still allocated. What's more... when I craete many threads
(3000) I can go down with memory for about 2 MB free... and what is
interesting after this level everything is deallocated correctly...
but it is not solution for my answer :/

Regards,

Michal
 
M

Michal

The memeory is managed by your OS. It may not be returned to the
system immediately but kept a little time for future allocation (it
saves requests to the system).

That's what usually happen under Linux and I expect the same happens
in Windows.

--
Hm... I'm not sure, but when I was waitnig for a few minutes... the
memory was still allocated. What's more... when I craete many threads
(3000) I can go down with memory for about 2 MB free... and what is
interesting after this level everything is deallocated correctly...
but it is not solution for my answer :/

Regards,

Michal
 
M

Michal

The memeory is managed by your OS. It may not be returned to the
system immediately but kept a little time for future allocation (it
saves requests to the system).

That's what usually happen under Linux and I expect the same happens
in Windows.

--
Hm... I'm not sure, but when I was waitnig for a few minutes... the
memory was still allocated. What's more... when I craete many threads
(3000) I can go down with memory for about 2 MB free... and what is
interesting after this level everything is deallocated correctly...
but it is not solution for my answer :/

Regards,

Michal
 
M

Michal

How do you "see" a memory leak?

GlobalMemoryStatus(&memInfo1);
memInfo.dwAvailPhys - there is an information how much free memory is
available in my system

Regards,

Michal
 
M

Michal

How do you "see" a memory leak?

GlobalMemoryStatus(&memInfo1);
memInfo.dwAvailPhys - there is an information how much free memory is
available in my system

Regards,

Michal
 
M

Michal

How do you "see" a memory leak?

GlobalMemoryStatus(&memInfo1);
memInfo.dwAvailPhys - there is an information how much free memory is
available in my system

Regards,

Michal
 
M

Michael Doubez

Hm... I'm not sure, but when I was waitnig for a few minutes... the
memory was still allocated. What's more... when I craete many threads
(3000) I can go down with memory for about 2 MB free... and what is
interesting after this level everything is deallocated correctly...
but it is not solution for my answer :/

4096 bytes of leak looks like the size allocated for the stack; you
may be in a mode where stacks of former threads are not reused. Or it
may be a simple bug (they call it a feature) from GlobalMemoryStatus
().

You say you can go dow to 2 MB free but do you actually run out of
memory ?
I expect the memory is reused when there is no other memory left.
 
S

Squeamizh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1








You should ask in a Windows group instead.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAksWOxAACgkQG6NzcAXitM+HygCfZXq+CMdgqiriYLbPx11GuPTD
8owAnRE8LDVxOiN09e3EY15nO80KPVkd
=DNzD
-----END PGP SIGNATURE-----

Your post was worse than worthless and your PGP stuff is obnoxious.
 
P

peter koch

Before I explain exactly where is the problem I will show my code:
(Platform - Windows CE 5, VisualStudio 2005)

The interesting part of source code is below:

/**************************************************************/
class classA {
        public:
        static DWORD WINAPI testingThread(LPVOID lpvoid);
        HANDLE m_hThread;
        void startthread();

};

DWORD WINAPI classA::testingThread(LPVOID lpvoid)
{
        return 1;

}

void classA::startthreadk()
{
        for(int counter = 0 ; counter < 100 ; counter++) {
                MEMORYSTATUS   memInfo;
                memInfo.dwLength = sizeof(memInfo);
                GlobalMemoryStatus(&memInfo);
                DWORD   dwThreadId = 0;
                m_hThread = CreateThread(NULL, 0, testingThread, this,
0,
&dwThreadId);
                DWORD dwRet = WaitForSingleObject(m_hThread,
INFINITE);
                if(WAIT_OBJECT_0 != dwRet) {
                        TRACE(_T("Something wrong !!! \n"));
                }
                int returnHandle = CloseHandle(m_hThread);
                TRACE(_T("After closing handle = %d
\n"),returnHandle);
                m_hThread = NULL;
                MEMORYSTATUS   memInfo1;
                memInfo1.dwLength = sizeof(memInfo1);
                GlobalMemoryStatus(&memInfo1);
                TRACE(_T("%d Test %d\n"),counter, memInfo..dwAvailPhys
-
memInfo1.dwAvailPhys);
        }

}

/**************************************************************/

The problem is that I don't know why but sometimes (usually one per
three times)
I can see the memory leak of 4096 bytes. Why? Do I do something wrong
with closing
thread? (Of course the program without creating thread works ok).
Closehandle
always returns 1, so everything should be ok.

I will be grateful for any response.

Michal

Three comments: First as Michael Tsang noted, this is Windows-specific
code: you are more likely to get an appropriate response there.
Secondly: if you increase the loop-count, does the memory usage
continue to grow? If not, oyu probably don't have a leak.
Last: You get a handle - don't you have to release it? Which takes me
back to the first point - ask in a Windows group. But read the
documentation first.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top