Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello once again =)

Lets say that I have an object (A) which encapsulates a different one (B). B has a method that takes a while to run (say, "marathon()") and B is not allocated dynamically. Then I start a thread to run marathon(). Lets say I do it like this:

C#
void foo() {
   A a;
   a.runB();  // Starts a thread, calls b.marathon()
}


The problem is: now "a" is dead, so "b" must be too. I haven't tryed it yet, but I would guess that this crashes, or is otherwise very unstable. I can't let the main thread hanging (say it's used for rendering and handling a GUI), so waiting is not an option. Is it possible to solve this without dynamic memory?

Best regards to all! ;)

PS: BTW, I couldn't find a forum specific for threading. Please let me know if there is one.
Posted
Updated 4-Feb-12 7:14am
v3

1 solution

I'd have A handle the thread, which calls B. But, you still need for your process to stay alive, but if it's a dll, and the app consuming it is alive, that should be fine, I think. I assume this is a winforms or WPF app ?
 
Share this answer
 
Comments
AndreFratelli 4-Feb-12 16:11pm    
It's neither, actually. The problem arose while developing a Qt application, but still, I'm interested in the more abstract idea of the object handling the thread dying before the thread does.
Christian Graus 4-Feb-12 17:41pm    
Ah, that is C++. See, I assumed it was C#, and the language does matter, because it has an impact on how memory is handled in general, and the lifetime of objects.
AndreFratelli 5-Feb-12 4:13am    
I posted in the C# forum? =\ didn't even realise..

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