CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2007
    Posts
    127

    WSAAsyncSelect and virtual functions

    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:
    Code:
    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!
    Last edited by Chirieac; May 24th, 2008 at 08:03 AM.
    I love this forum. Thanks all for your help!

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: WSAAsyncSelect and virtual functions

    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....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    May 2007
    Posts
    127

    Re: WSAAsyncSelect and virtual functions

    Thnks mister Wizard! I will try that... Bye!
    I love this forum. Thanks all for your help!

  4. #4
    Join Date
    May 2007
    Posts
    127

    Re: WSAAsyncSelect and virtual functions

    Now, doing that I get an error on my main message while loop, at DispatchMessage (&msg) ;
    Weird stuff...
    I love this forum. Thanks all for your help!

  5. #5
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: WSAAsyncSelect and virtual functions

    Quote Originally Posted by Chirieac
    Hello!
    ...I call virtual functions like OnAccept from WndProc...
    OnAccept() shouldn't be called by application. It should be called by framework.
    henky
    ----------------------------------
    [email protected] is not my email address anymore...
    Jangan Pernah Menyerah

  6. #6
    Join Date
    May 2007
    Posts
    127

    Re: WSAAsyncSelect and virtual functions

    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?
    I love this forum. Thanks all for your help!

  7. #7
    Join Date
    May 2007
    Posts
    127

    Resolved

    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, [email protected]) 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!
    I love this forum. Thanks all for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured