Memory leaks with multiple threads

Greetings,

I am writing a multithreaded program that receives connections from a socket within a thread and then creates another "child" thread to handle futher communications on that socket. Then the "father" thread goes back to waiting for more connection attempts. When the "child" thread finishes its operations I close the socket handle, delete dynamic memory, and do an ExitThread. The problem is that everytime this happens the memory is not released. Shouldn't all memory be released after the "child" thread is done. Also, note that up to 60 "child" threads may be created and terminated at the same time so the memory is creeping up very quickly? Does ExitThread unload the threads stack memory automatically?

Please, any help would be appreciated.
sinisterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
>> ExitThread unload the threads stack memory
>> automatically
yes and other OS thread-related resources

However, from a C++ process you shoudl not use ExitThread() (Or CreateThread())  You should be using _beginthread() and _endthread(). These initialize and clean-up the C++ runtime library for the thread.  (Thus _endthread() may free memory.)   I suspect that if you make this change your problems will go away (certainly other problems will.)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sinisterAuthor Commented:
Thanks nietod;

I believe that your suggestion helped to reduce the memory leakage. Most of the memory problems were a result of not delete several structures properly. What is the correct way to free the memory. Here is what I had to create the structure:

SomeSTRUCT * myStruct = new SomeStruct

And to delete it I was using:

delete myStruct

Is this correct? I managed to use a workaround to fix the problem but I'm not sure of the above procedure.
0
nietodCommented:
That is correct.

If it is an array of something, you have to use "delete []"  instead of "delete"  Like

SomeStruct *MyStructArray = new SomeStruct[100];

delete [] MyStructArray;

What workaround did you use?
0
sinisterAuthor Commented:
Actually, I made a stupid mistake, I was de-referencing the pointer to another existing structure and then later deleting the pointer, but, ofcourse the original structure still existed.

Thanks for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.