Home » U++ Library support » U++ Widgets - General questions or Mixed problems » thread, virtual class and exception
|
Re: thread, virtual class and exception [message #15175 is a reply to message #15171] |
Mon, 07 April 2008 12:53   |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
I'm afraid your example isn't terribly clear, but I think I can guess what you are trying to do.
You have a server, and various types of socket class. The server listens for a connection, creates the correct type of class to handle it and then runs a thread, passing the class object. These socket classes throw exceptions that you need to display on the GUI. Is this correct?
I think the main problem is that you are attempting to update the GUI from the thread. In Upp only the main thread (the server in this example) is allowed to do this. Also attempting to pass the messages up by exceptions is a bad idea, what you should do instead is pass a callback to a function in the main thread that can handle to GUI updates:
void fc(void *ss, Callback1<String> WhenException); // Thread function
void OnException(String exc); // Updates GUI
I'd also try and avoid creating objects with new, passing as void * and then recasting. you could for instance use the One<> container for this:
void fc(One<base_class> obj, Callback1<String> WhenException); // Thread function
..
One<base_class> obj;
obj = new inheritor(a, b, c);
thread.Run(THISBACK2(fc, obj, THISBACK(OnException));
This avoids the heap-leak as the One container takes ownership of the pointer and deletes it when the thread finishes.
One reason this may not have been working before is that you missed the virtual keyword from the vt() function decl. in the clss class.
Hope that helps, I rewrote your example to check that this all works so I've attached my test if it's any use.
James
-
Attachment: test.zip
(Size: 2.51KB, Downloaded 294 times)
[Updated on: Mon, 07 April 2008 12:55] Report message to a moderator
|
|
|
Re: thread, virtual class and exception [message #15185 is a reply to message #15175] |
Tue, 08 April 2008 08:00   |
 |
bonami
Messages: 186 Registered: June 2007 Location: Beijing
|
Experienced Member |
|
|
thank you so much, James. after using this callback feature this small program does not crash. but my real project remains same.
i renamed my testcase as testme. it runs well. but after changing line 99, 74 and 107("ss" to "ez"), it crashes (containing my real project's code). and after changing back, it crashes, too.
so i wonder whether there are more subtle problems. or have i some mistakes in understaning you into my code?
i was amazed you guessed my project's purpose, since i just hid the details in order to show my problem.
not yet studied the One, but because i need to track all the objects, i think i will probably not use it.
-
Attachment: test.zip
(Size: 29.98KB, Downloaded 281 times)
[Updated on: Tue, 08 April 2008 09:37] Report message to a moderator
|
|
|
|
|
Goto Forum:
Current Time: Mon Apr 28 10:51:58 CEST 2025
Total time taken to generate the page: 0.00939 seconds
|