I cannot for the life of me see where i'm going wrong.
I've got a winsock class, the SOCKET is defined in the Header, but when I try to use it in a function I get a first exception crash. It's ground me to a halt (btw, for anyone who saw the other thread, I solved my win32 wrapper problem).

Code:

//Header://

#ifndef _WINSOCKCLASS_H_
#define _WINSOCKCLASS_H_
#define _WINSOCKAPI_ 

#include <WinSock2.h>
#include <String>
#include <Windows.h>

class winSockClass
{
public:
    winSockClass();
    ~winSockClass();
    bool AsyncSelect( HWND lHwnd );
    bool Startup();
    bool wsConnect(std::string URL, int lPort);
    bool SendMsg(std::string message, LPARAM lParam);
    bool Shutdown();

    std::string lServer;
    int lPort;
private:
    SOCKET lSocket;
    SOCKADDR_IN sockAddr;
    WSADATA wsaDat;
    struct hostent *host;
};

#endif _WINSOCKCLASS_H_


// CPP: //
#define _WINSOCKAPI_ 
#include "winSockClass.h"
#include <WinSock2.h>
#include <vector>

#pragma comment(lib, "ws2_32.lib")

winSockClass::winSockClass()
{
    lSocket = NULL;
}

winSockClass::~winSockClass() {}

bool winSockClass::Startup()
{
    WSADATA wsaa;
    int nResult = WSAStartup(MAKEWORD(2,2), &wsaa);
    if(nResult != 0)
    {

        return false;
    }

    lSocket = socket( AF_INET, SOCK_STREAM, 0 );  // <<--- crash here.
    if( lSocket == INVALID_SOCKET )
    {
        return false;
    }

    return true;
}

Dani AI

Generated

Short answer: this is almost certainly not a Winsock bug but an invalid object pointer or memory corruption. The fact that a locally declared SOCKET works yet assigning into the member lSocket triggers a crash strongly suggests the write this->lSocket = ... is happening against an invalid this (object) address.

As suggested, verifying WSAStartup is good — and since you already tried that, next check the object itself. Reproduce and inspect at runtime:

  • Put a breakpoint at the top of Startup() and print the this pointer and the address of the member before calling socket(). Example quick checks:
    printf("Startup entered: this=%p, &lSocket=%p\n", (void*)this, (void*)&lSocket);
    lSocket = INVALID_SOCKET;   // does this write crash?

    If this shows common debug-fill values (0xCCCCCCCC, 0xCDCDCDCD, 0xFEEEFEEE) or an otherwise bogus pointer, the instance was never constructed, has been freed, or memory is being stomped.

Things to inspect next:

  • How and where the winSockClass instance is created and stored (stack vs heap, lifetime across threads). Make sure you are not calling Startup() on a pointer that was never new-ed or already deleted.
  • Look for buffer overruns or other writes that could corrupt the object memory (watch nearby members like lServer, sockAddr).
  • Try a minimal test program that only constructs the class and calls Startup() to see if the problem reproduces in isolation.

If those checks are inconclusive, post the small calling code and the debugger call stack at the crash. The call stack and the this value when entering Startup() usually reveal whether Winsock is at fault (rare) or your object lifetime/ memory is.

Recommended Answers

All 2 Replies

How about verifying that the Winsock DLL does in fact support 2.2 as indicated below?

int nResult = WSAStartup(MAKEWORD(2,2), &wsaa);
if(nResult != 0)
{
return false;
}

/* Confirm that the WinSock DLL supports 2.2.*/

if (LOBYTE(wsaa.wVersion) != 2 || HIBYTE(wsaa.wVersion) != 2) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        printf("Could not find a usable version of Winsock.dll\n");
        WSACleanup();
        return 1;
    }
    else
        printf("The Winsock 2.2 dll was found okay\n");

lSocket = socket( AF_INET, SOCK_STREAM, 0 ); // <<--- crash here.

Just implemented that, but still no difference.
if I define a SOCKET sock; just above the socket() line and use that instead of lSocket, it's fine, but then I couldn't use sock again in any other function.

I just don't understand why it's having trouble with lSocket. It's defined properly in the class...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.