Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As the title,i use malloc in a windows thread(created by CreateThread API) and free it in calling function which call the CreateThread function and it works fine first. But when i started a new thread , it throws a access exception. And in Debug mode it has no problem the same as debug the release version but running the release version it throws the exception.
Posted
Comments
Mohibur Rashid 18-Apr-13 23:28pm    
Make sure you are not using more than you have allocated, it happens mostly with arrays, if you try to delete memory which has been deleted already will also trouble you. so far that's all I can recall that bugged me
nv3 19-Apr-13 2:20am    
Without any code and just by the rough description you gave, it will be difficult to help you.
Malli_S 19-Apr-13 3:21am    
It would be easy if you could post the thread function.
Kucera Jan 19-Apr-13 9:56am    
Dear "friend",

next time it would be pollite to ASK before you start to "reconstruct" my articles/answers.
E.g. you completely changed the meaning of "Pattern II." of my answer. Further, the code was not buggy... maybe you just have to modify it a bit before you use it, thats all.

Next time, discuss before updating OR post your own answer.

Regards,
J. K.
Malli_S 22-Apr-13 2:26am    
Yup. :D Sure dear.

You're right, I missed CPallini's 5 point this way. Should I restore the ans? Or you happy with the points?

1 solution

Hi,

what you described is simply a bad design. You shall no malloc the ThreadFunction and free the memeory in the calling function as you described. This can cause problems especially in multithreaded applications. Instead you shall use the following pattern:

Pattern I.
- use malloc to allocate memory in the calling thread.
- then pass the pointer to the allocated memory to the newly created thread (I suppose the new thread fill some data in it, or so)
- after the thread finishes, in the calling thread use the data that hase been stored in the memory.
- finally, by using synchronization method in the calling thread check for the exit of new thread. Deallocate the memory if it has exited.

Pattern II.
- Leave memory handling to newly created thread. The newly created thread will allocate the required memory and release it before exiting.

If the memory shall be shared among all threads, then you need to allocate the memory before creating the threads and release it after all the threads have finished (you have to use the thread synchronization mechanism). Be aware that you need to synchronize the threads with some thread synchronization mechanism !

Every time you must make sure that:
- for every malloc you call free
- for every new you call delete
- for every new[] you call delete [].

Hope this helps.

Best regards,

J. K.

[Updates: Restructured the entire answer. Removed buggy code and added new stuff as well.]
 
Share this answer
 
v3
Comments
CPallini 19-Apr-13 3:35am    
My 5.
Keanu L 19-Apr-13 4:04am    
Of Course i do as you say , i allocate the memory in calling function and free it in calling function .The Debug Version works fine ,and when i debug the Release Version ,it has no exception but when running the release version ,it throws the exception.
************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900