c++networkingvisual-c++

C++ GameNetworkingSockets running results in null pointer cannot point to a block of non-zero size


I'm working on a chat application using C++ and the GameNetworkingSockets library for networking in Visual Studio 2022. I've built the library according to the github page (github) and then set up my project including the .lib file and all dependencies in my Project Settings.

Reproducible Example: I have created a reproducible example (as far as i have understood what it is)

#include "steam/steamnetworkingsockets.h"
int main() {
    // Initialize the SteamNetworkingSockets library
    SteamDatagramErrMsg errMsg;
    GameNetworkingSockets_Init(nullptr, errMsg);

    // Create the ISteamNetworkingSockets interface
    ISteamNetworkingSockets* m_Interface = SteamNetworkingSockets();

    // Set up the server address
    SteamNetworkingIPAddr serverAddress;
    serverAddress.Clear();
    serverAddress.ParseString("127.0.0.1");
    serverAddress.m_port = 27020;

    // Display the server address
    char szAddr[SteamNetworkingIPAddr::k_cchMaxString];
    serverAddress.ToString(szAddr, sizeof(szAddr), true);
    //Printf("Connecting to server at %s\n", szAddr);

    // Attempt to connect
    HSteamNetConnection m_Connection = m_Interface->ConnectByIPAddress(serverAddress, 0, nullptr);
    m_Connection == k_HSteamNetConnection_Invalid;

    // Cleanup
    GameNetworkingSockets_Kill();

    return 0;
}

Running this will result in the following assertion failing (xmemory file):

_CONSTEXPR20 void deallocate(_Ty* const _Ptr, const size_t _Count) {
    _STL_ASSERT(_Ptr != nullptr || _Count == 0, "null pointer cannot point to a block of non-zero size");
    // no overflow check on the following multiply; we assume _Allocate did that check
    _Deallocate<_New_alignof<_Ty>>(_Ptr, sizeof(_Ty) * _Count);
}

With the following Error Message:

Debug Assertion Failed!

Program: C:\Users\alexl\source\repos\Chat\x64\Debug\Chat.exe
File: D:\VisualStudio\VC\Tools\MSVC\14.36.32532\include\xmemory
Line: 944

Expression: null pointer cannot point to a block of non-zero size

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

Call stack:

>   Chat.exe!std::allocator<char>::deallocate(char * const _Ptr, const unsigned __int64 _Count) Line 944    C++
    Chat.exe!std::string::_Tidy_deallocate() Line 4877  C++
    Chat.exe!std::string::~basic_string<char,std::char_traits<char>,std::allocator<char>>() Line 3159   C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::SetLocalCertUnsigned() Line 1314   C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::BThinkCryptoReady(__int64 usecNow) Line 1090   C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::InitConnectionCrypto(__int64 usecNow) Line 1029    C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::BInitConnection(__int64 usecNow, int nOptions, const SteamNetworkingConfigValue_t * pOptions, char[1024] & errMsg) Line 987    C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionUDP::BInitConnect(const SteamNetworkingIPAddr & addressRemote, int nOptions, const SteamNetworkingConfigValue_t * pOptions, char[1024] & errMsg) Line 1199   C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkingSockets::ConnectByIPAddress(const SteamNetworkingIPAddr & address, int nOptions, const SteamNetworkingConfigValue_t * pOptions) Line 1135   C++
    Chat.exe!main() Line 140    C++
    [External Code] 

I have noticed that at the point where ~basic_string is called the this std::string pointer is NULL: + this 0x0000003927f2ee38 <NULL> std::string *. Is a string destructor being called on a non-existent string?

Maybe it has to do with how i set up my project with linking all the dependencies or the version of some libraries because everything else should be working fine and i am only using one function ConnectByIPAddress() where it seems to go wrong.

Instead of the Debug Assertion failed error, sometimes i will get read or write access violation exception also in xmemory file:

_CONSTEXPR20 void _Container_base12::_Orphan_all_unlocked_v3() noexcept {
    if (!_Myproxy) { // no proxy, already done
        return;
    }

    // proxy allocated, drain it
    for (auto _Pnext = _STD exchange(_Myproxy->_Myfirstiter, nullptr); _Pnext; _Pnext = _Pnext->_Mynextiter) {
        _Pnext->_Myproxy = nullptr;
    }
}

Call Stack:

>   Chat.exe!std::_Container_base12::_Orphan_all_unlocked_v3() Line 1349    C++
    Chat.exe!std::_Container_base12::_Orphan_all_locked_v3() Line 1205  C++
    Chat.exe!std::_Container_base12::_Orphan_all() Line 1364    C++
    Chat.exe!std::string::_Tidy_deallocate() Line 4868  C++
    Chat.exe!std::string::~basic_string<char,std::char_traits<char>,std::allocator<char>>() Line 3159   C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::SetLocalCertUnsigned() Line 1314   C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::BThinkCryptoReady(__int64 usecNow) Line 1090   C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::InitConnectionCrypto(__int64 usecNow) Line 1029    C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::BInitConnection(__int64 usecNow, int nOptions, const SteamNetworkingConfigValue_t * pOptions, char[1024] & errMsg) Line 987    C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkConnectionUDP::BInitConnect(const SteamNetworkingIPAddr & addressRemote, int nOptions, const SteamNetworkingConfigValue_t * pOptions, char[1024] & errMsg) Line 1199   C++
    Chat.exe!SteamNetworkingSocketsLib::CSteamNetworkingSockets::ConnectByIPAddress(const SteamNetworkingIPAddr & address, int nOptions, const SteamNetworkingConfigValue_t * pOptions) Line 1135   C++
    Chat.exe!main() Line 140    C++
    [External Code] 

Error Message:

Exception thrown: write access violation.
**_Pnext** was 0x20.

Solution

  • So the problem really was with my setup of the project. I don't exactly know where or what but it seems i might have missed to include some of the dependencies of the networking library i am using. I have created a new project, included every dependency in the project settings and now it's working fine. That could have been avoided pretty easily but now i know better.