-
February 17th, 2020, 05:46 AM
#1
Stop a thread
Please explain me why the following thread when is forced to stop is not cleaning the heap:
// here is starting thread
Code:
BOOL CMyDoc::ReadHDD(CStringA sVolume)
{
THREADSTRUCT* pTS = new THREADSTRUCT;
pTS->m_pDoc = this;
pTS->m_sVolume = sVolume;
m_pWinThread = AfxBeginThread(&CMyDoc::ReadThread, (LPVOID)pTS, THREAD_PRIORITY_BELOW_NORMAL, CREATE_SUSPENDED, 0, NULL);
m_pWinThread->m_bAutoDelete = TRUE;
m_pWinThread->ResumeThread();
return TRUE;
}
and
Code:
UINT CMyDoc::ReadThread(LPVOID lpParam)
{
THREADSTRUCT* pTS = (THREADSTRUCT*)lpParam;
if (NULL == pTS)
return 0;
//.... code that takes long time, no returning call
delete pTS;
return 1;
}
where
Code:
// inside my CMyDoc header
protected:
CWinThread* m_pWinThread;
struct THREADSTRUCT
{
CMyDoc* m_pDoc;
CStringA m_sVolume;
};
what is happen: if I let that the thread to naturally close it, I mean after the thread is ended and afterwards close my app, everything is ok, I got no memory leak. But if I close my app when the thread is alive (while code that takes long time from the thread is executing), I got memory leak, of course due to pTS object from the heap.
My question, what is happen when the thread is stopped while that "code that takes long time" is alive ?
And how to be assured that pTS is properly delete it ?
P.S.
Of course, I can live without THREADSTRUCT, it is just for testing and learning purpose.
P.S.2
Of course, I can make pTS as CMyDoc member and delete it inside CMyDoc destructor. But I want to understand what happen in this situation.
Last edited by mesajflaviu; February 17th, 2020 at 06:39 AM.
-
February 17th, 2020, 07:34 AM
#2
Re: Stop a thread
Thread when is forced to stop
How is the thread stopped? If it is terminated elsewhere, then the heap memory is not automatically freed. Threads need to terminate themselves. If it is required that one thread terminates another then this is usually done by signalling the thread that it is to terminate. See http://www.flounder.com/processes.htm for more info.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 17th, 2020, 07:52 AM
#3
Re: Stop a thread
"Thread when is forced to stop" I mean I close the whole app when thread is alive.
-
February 17th, 2020, 07:55 AM
#4
Re: Stop a thread
Before exiting the main App you have to let your worker threads exit.
Your main thread can SetEvent for exiting an the worker threads have periodically wait for this event and if it has been set then exit (gracefully!) and notify the main thread about the exiting.
See also about Using Worker Threads
Victor Nijegorodov
-
February 17th, 2020, 08:12 AM
#5
Re: Stop a thread
"worker threads have periodically wait for this event" this one I don't know how to implement it ... because that code from the thread is in fact a simple call:
Code:
UINT CMyDoc::ReadThread(LPVOID lpParam)
{
....
some_function_that_takes_long_time();
...
}
where should I put WaitForSingleObject ? Before this function, or after ? I guess after ... but this mean that my app is not closing until this thread is done ?
Last edited by mesajflaviu; February 17th, 2020 at 08:14 AM.
-
February 17th, 2020, 08:37 AM
#6
Re: Stop a thread
I have posted you a link to a great Joe Newcomer's essay about using worker threads.
Please, check it out!
Victor Nijegorodov
-
February 17th, 2020, 09:43 AM
#7
Re: Stop a thread
You need to restructure your code. Use an exit event as others have mentioned and inside the
Code:
some_function_that_takes_long_time();
function, frequently check if the exit event has been set. If it has return a false from the function, and exit the thread if the function has returned false.
-
February 17th, 2020, 10:23 AM
#8
Re: Stop a thread
The fact is that some_function_that_takes_long_time function is a simple call into a C library which is not mine ... I don't know what option do I have ...
-
February 17th, 2020, 10:30 AM
#9
Re: Stop a thread
Originally Posted by mesajflaviu
The fact is that some_function_that_takes_long_time function is a simple call into a C library which is not mine ... I don't know what option do I have ...
And what is its name?
And what is that function supposed to do?
Victor Nijegorodov
-
February 17th, 2020, 10:47 AM
#10
Re: Stop a thread
Originally Posted by VictorN
And what is its name?
And what is that function supposed to do?
And does that function have an alternative that allows it to be cancelled?
-
February 17th, 2020, 11:01 AM
#11
Re: Stop a thread
Originally Posted by VictorN
And what is its name?
And what is that function supposed to do?
That function read specific file on HDD drive. Its name: GetFileAvailability(...)
-
February 17th, 2020, 11:03 AM
#12
Re: Stop a thread
Originally Posted by Arjay
And does that function have an alternative that allows it to be cancelled?
No, has no option to be cancelled ... I guess that putting a WaitForSingleObject after his call will solve ML issue ... is a step forward ...
-
February 17th, 2020, 11:47 AM
#13
Re: Stop a thread
Originally Posted by mesajflaviu
That function read specific file on HDD drive. Its name: GetFileAvailability(...)
So it is a user defined function, isn't if?
Why does it require long time to run? How does it get/check the "FileAvailability"?
Could you use some asynchronous function instead?
Victor Nijegorodov
-
February 18th, 2020, 02:02 AM
#14
Re: Stop a thread
some_function_that_takes_long_time is a function inside a C library ... I have the source code, but inside is a long string of C functions that read some specific files on HDD device. In his reading process, deep inside, maybe I could put my code, but is not an easy thing due to language barrier. Strange to me is that when I stop my app while this function is alive, the thread is stopped before arrive to his ending ...
-
February 18th, 2020, 05:55 AM
#15
Re: Stop a thread
If GetFileAvailability() is not doing as expected (eg not returning etc), then it's likely that there is a problem with that function. Have you stepped through that function with the debugger to see why it is not returning within an expected time? It sounds like there is an infinite loop somewhere that is keep retrying some action until it succeeds which doesn't happen.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
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
|