Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Threads: Will memory allocated be deallocated after execution?

On my pc I had experimented with the CreateThread function that creates a thread. I tried something like this in main() program and in the ThreadFunc()

[code]
char buffer[80];
wsprintf(buffer,"var1 = %d , var2 = %d", var1, var2);
[/code]

I have not explicitly allocated memory for "buffer". But it seemed to work in main(), but it did not work ThreadFunc(). Why is this so?

I am writing this post because I am concerned about memory leaks. If I had allocated memory in ThreadFunc() (but not deallocated it), and the thread function completes its execution, would the memory I had allocated be deallocated.
--deostroll

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : On my pc I had experimented with the CreateThread function that
    : creates a thread. I tried something like this in main() program and
    : in the ThreadFunc()
    :
    : [code]:
    : char buffer[80];
    : wsprintf(buffer,"var1 = %d , var2 = %d", var1, var2);
    : [/code]:
    :
    : I have not explicitly allocated memory for "buffer". But it seemed
    : to work in main(), but it did not work ThreadFunc(). Why is this so?
    :
    : I am writing this post because I am concerned about memory leaks. If
    : I had allocated memory in ThreadFunc() (but not deallocated it), and
    : the thread function completes its execution, would the memory I had
    : allocated be deallocated.
    : --deostroll

    [color=Blue]
    Memory leaks will appear only if you allocate memory dynamically, by malloc() or new. The way you allocate a buffer is just a room on stack, so it cannot be a leak.

    However, if a thread will allocate memory dynamically - the same thread should deallocate memory accordingly. It is even preferable to use the separate heap object (HeapCreate API) for a thread, so there will be no collisions with other threads and faster allocation/deallocation too.

    Also, remember, that the correct way to end a thread is simply return from thread callback function. The use of ExitThread API is bad, because resources allocated by a thread may leak in this case.
    [/color]
  • LundinLundin Member Posts: 3,711
    As a sidenote, you may get memory leaks when using CreateThread together with C runtime libraries. From the Win32 documentation:

    "A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called."

    Whether this peculiar statement applies to a certain compiler (ie Visual C) or to all C compilers, I have no idea. Better safe than sorry.

    I would imagine that some C function needs dynamic allocation to handle thread safety or something like that, and it is assuming that endthread() is used to clean up the heap afterwards.

    This is one of the things I simply do without knowing the reason why, it would be interesting to hear the reason if anyone knows it.
  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : This is one of the things I simply do without knowing the reason
    : why, it would be interesting to hear the reason if anyone knows it.

    Probably looking at the source code for beginthread() and endthread() will give an explanation.
    At some point, these functions will call CreateThread/ExitThread, but not before doing some further initialization.

    I can only guess that the memory leaks would come from C runtime-library functions using dynamic memory allocation which sometimes 'leak' if certain initialization (eg beginthread) is not done. Why they would do this, I can not begin to guess.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    This one link is very explanatory (check some information on ExitThread() and the like):

    [link=http://www.flounder.com/badprogram.htm]http://www.flounder.com/badprogram.htm[/link]
  • LundinLundin Member Posts: 3,711
    Interesting link. Still he doesn't explain why there will be memory leaks. According to that link, the sole issue is thread-safety and nothing else. But everyone knows (should know) that C libraries aren't thread-safe, nothing new there. Though if I only call a particular C function just from one place in the whole code, there will be no thread safety issues.

    And according to that link, the problem only applies to Visual Studio, because it is too dumb to pick a multi-threaded library when thread functions are used in the code. If you use a non-MS compiler, you won't have that problem.

    So if none of it has anything to do with memory leaks, why do they scare us with them in the Win API docs?
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : So if none of it has anything to do with memory leaks, why do they
    : scare us with them in the Win API docs?

    [color=Blue]Probably, because Visual Studio (or MS Compilers in general) has the most market share, so the remark in MSDN will cause less amount of buggy programs out there.[/color]
Sign In or Register to comment.