Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hallo.
i also wanted to add additional information.
for the above mentioned programm. how could i dellocated this array when i use follow.
first it initialize a:

for (int i = 0; i < 100; i++){
joblist[i] = new JobOptions(some parameter);
}
while (true){
for (int i = 0; i < 100; i++){
Sleep(200);
if ((run == true) && testnumber< number) {
AfxBeginThread(VTKAppThread,(LPVOID)joblist[i]);
}
}
}


my question is: How can i delocate the object "joblist" inside the while-loop?

Edit:
Sorry for the high number of reviews. My computer got crazy
Posted
Updated 22-Feb-10 5:56am
v8

Why would you want to deallocate it there? This whole thing looks kinda shaky to me. Where does execution ever leave the while loop? Where do testnumber and number ever change? Why are the 100s hard coded?

Since you're passing the pointers in joblist to the thread function you can't safely delete them until you're sure that the worker threads are finished or are no longer using them. There are plenty of ways to do this, one would be to keep track of the returns from AfxBeginThread and use WaitForMultipleObjects to wait for all of the thread handles to become signaled. Another would be to use some thread synchronization to protect the pointers in joblist individually and then wait for the worker threads to mark them as "unused".
 
Share this answer
 
Because you're starting threads, you can't. Those threads are not letting you know when they finish their work, so you can't deallocate something that could still be getting used.

I agree with the other guy, the whole thing looks really bad to me.
 
Share this answer
 
You could start a thread by AfxBeginThread(pfThread, new Job(..))

and let it (the thread) delete its parameter at exit from pfThread :)
 
Share this answer
 
Well, I don't think you will find a lot of programs which purpose is to crash...
But as the previous poster said, your code is really a mess and it probably doesn't make any sense. It would help a lot if you could explain what you are trying to achieve EXACTLY.
You could also answer the questions that were asked in the previous reply.
 
Share this answer
 
you are righ but the purpose of my program is to work no stop.and not to crash.
 
Share this answer
 

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