-1

I get a strange error complaining about stack corruption I'm assuming, and I have debugged it a bit but I haven't found out the issue. I also can't seem to implement nothrow in Visual Studio 2010!

XYZ::XYZ(char * d)
{
    hostname = new char[HOSTNAME_LENGTH];
    ip = new char[IP_ADDR_LENGTH];

    /*Dynamic Memory*/
    memset(hostname, 0, HOSTNAME_LENGTH);
    memset(ip, 0, IP_ADDR_LENGTH);

    //strncpy(hostname, d, HOSTNAME_LENGTH);

    if(dWSAStartup(MAKEWORD(2,2), &wsd) == 0) //Crashes Here!
    //And so on..

dWSAStartup is dynamically linked from ws2_32.dll and has correct function parameters typecasted:

typedef int (*WSAS)(WORD, LPWSADATA); //WSAStartup

And no, the FreeLibrary function hasn't been called -- so the function pointer IS valid!

This bug is the only thing stopping me! Would anyone throw me a pointer?

Saustin
  • 1,117
  • 6
  • 15
  • 27
  • Allergic to `std::string`? What's `wsd`? What's the relevant of those dynamic `char` buffers to your question? Where does `WSAS` come into it? I'll just get out my ESP. – Lightness Races in Orbit Jul 16 '11 at 14:40
  • Sadly, yes I am. I included that because I knew someone would complain if I didn't show it. This is a dynamically linked function, so WSAS is the pointer to the function inside of memory. – Saustin Jul 16 '11 at 14:42
  • You can try to rebuild your application, this witchery helps sometimes. – Vlad Jul 16 '11 at 14:43
  • @Saustin: Why? And why did you ignore the rest of my questions? – Lightness Races in Orbit Jul 16 '11 at 14:44
  • It's called lapped responses :) And different programmers have different habits -- the issue isn't that I don't use std::string, it's that I'm getting a runtime error. I have rebuilt the application, no dice. – Saustin Jul 16 '11 at 14:44
  • http://stackoverflow.com/questions/142644/weird-msc-8-0-error-the-value-of-esp-was-not-properly-saved-across-a-function-c http://stackoverflow.com/questions/1465207/run-time-check-failure-0-the-value-of-esp-was-not-properly-saved-across-a-fun http://stackoverflow.com/questions/1588594/how-to-debug-value-of-esp-was-not-saved-across-function-call-error http://stackoverflow.com/questions/1933289/esp-error-when-i-call-an-api-function – celavek Jul 16 '11 at 14:45
  • The only one of those remotely related to my situation is the one about loading songs from a file. I'll try that windows debug tool in a second. – Saustin Jul 16 '11 at 14:51
  • From one of the linked questions: "Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention." – Bo Persson Jul 16 '11 at 14:51
  • Not talking in the sense of that, I'm talking in the sense of dynamically linked functions. Yes, all 4 of those do have the same errors, but only one of them involved dynamically linked functions -- the other one is coded in C#. – Saustin Jul 16 '11 at 14:54
  • Windbg won't function on my computer as well. – Saustin Jul 16 '11 at 14:58

1 Answers1

1

typedef int (*WSAS)(WORD, LPWSADATA); //WSAStartup

That's wrong, the calling convention is missing. It defaults to __cdecl which is not the way it is declared in winsock2.h. Which is why you are getting the runtime diagnostic, after the call it pops the arguments off the stack, something that was already done by WSAStartup(). An imbalanced stack is the result. Fix:

typedef int (__stdcall * WSAS)(WORD, LPWSADATA); //WSAStartup

The actual declarator used is FAR PASCAL, networking apis are strongly preserved from the previous century. Give your compiler a bit of love for warning you about this, imbalanced stacks are extremely hard to diagnose without the auto-generated debugging code.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Wow! Perfect! I didn't think the fix would be *that* easy! I should have looked at the declaration inside of the winsocks header, thanks. – Saustin Jul 16 '11 at 15:03