Click to See Complete Forum and Search --> : WSAAsyncSelect and virtual functions


Chirieac
May 24th, 2008, 08:01 AM
Hello!
I'm trying to make an asynchronous socket class with WSAAsyncSelect.
I get an unhandled exception on this events when I call virtual functions like OnAccept from WndProc. I have the pointer for the instance of the class in the window: SetWindowLong(hwnd, GWL_USERDATA, (long) ((LPCREATESTRUCT)lParam)->lpCreateParams);
If I try to connect to a socket that is listening I get an error.
If I remove the keyword virtual from the function and try now to connect the Onaccept is called with no problem, but I need those function to be virtual.
What is the problem?

Thanks for reading my post, bye!

TheCPUWizard
May 24th, 2008, 08:08 AM
Remember....

A non-virtual function has a "real" address.

A virtual function is located indirectly via a lookup table (vTable) and can change based upon the objects actual type.

What is wrong with having a NON-virtual function in your base class which simply calls the virtual function. This will isolate the "virtuality" of the actual function....

Chirieac
May 24th, 2008, 08:22 AM
Thnks mister Wizard! I will try that... Bye!

Chirieac
May 24th, 2008, 08:30 AM
Now, doing that I get an error on my main message while loop, at DispatchMessage (&msg) ;
Weird stuff...

henky@nok.co.id
May 24th, 2008, 04:28 PM
Hello!
...I call virtual functions like OnAccept from WndProc...
OnAccept() shouldn't be called by application. It should be called by framework.

Chirieac
May 24th, 2008, 04:37 PM
Well, WSAAsyncSelect send a custom message to a window and the LOWORD of the lParam contains one of FD_xxx events (like FD_ACCEPT). On FD_ACCEPT I must call the function OnAccept, right? Is there another way?

Chirieac
May 24th, 2008, 05:00 PM
Stupid me... I've created the socket class in a function and guess what? After the function return the class object was deleted! On FD_WRITE when I try to get that class instance pointer, that pointer now contains garbage data....

But why if I make OnAccept non-virtual I still can access that function through that "invalid" pointer and get no errors?

Sorry guys (TheCPUWizard, henky@nok.co.id) for my useless post... I hope that in the future I will make better posts that other persons can learn from.

Thanks and sorry, Bye!