Issue using FSocket pointers as member variables


I have been following useful examples on establishing TCP socket listeners, but I am having serious problems with the implementation.

Essentially, I define a FSocket pointer as a member variable in the header of my class, but whenever I try to set it in the cpp file I get a write access violation because it is a null pointer.

.h file:

FSocket* m_listenerSocket;

.cpp file:

FSocket* newSocket = socketBuilder.Build();
m_listenerSocket = newSocket;

The variable ‘newSocket’ is created fine, but as soon as I try to assign it to ‘m_listenerSocket’ I get the following error message: “Exception thrown: write access violation. this was nullptr.”. I’ve tried replacing the line shown in the header file with:

FSocket* m_listenerSocket = 0;


FSocket* m_listenerSocket = nullptr;

Because I know some compilers can get fussy about that, but no matter what I try nothing works.

If its saying “this was a nullptr” it means that the pointer to the class is invalid aka this->m_listenerSocket.
So the class is not constructed properly or you are calling the code somewhere you shouldnt be.

Where are you calling “m_listenerSocket = newSocket;”?
Also, is it a UObject derived class?

Thanks for pointing me in the right direction. I’m creating an instance of the UObject I’m calling the method on in the constructor of my GameInstance and it appears to be set to null by the time I’m calling the method.