Garbage Collector messing with my FSocket?

Hello guys,

After countless hours of trying to figure out what’s up with this one, I’m coming over to share the fun!

I have a “custom” UNetworkConnection class (made it child of UObject). Inside, I have a few methods, such as ReadPackets, WritePackets, Update, etc. And a FSocket, used to connect to my game server. I also have a PlayerController C++ class, extended by a PlayerController Blueprint. In the PC C++ class, I have methods such as Connect, Disconnect, Update - all of them exposed to blueprints… which calls my UNetworkConnection class methods.

No issue connecting, sending packets, reading packets, however, after a short time - between 10 to 60 seconds, things go nuts and here’s what happens:

http://puu.sh/p6xYp/1322d343a5.png

It appears the FSocket from my UNetworkConnection somehow gets dropped by the garbage collector for some reasons. It looks like it’s null. Yet we’re passing through the nullptr check.

I can’t explain it. I just don’t get it. Apparently we need to make things UProperty as to ensure the garbage collectors do not dumbly recycle things that actually are still referenced and in use, but that is possible only for UObjects and… FSocket isn’t one.

The pointer to the FSocket isn’t nulled anywhere by myself. The reference is always there. I landed on a post from (TCP Socket Listener, Receiving Binary Data into UE4 From a Python Script! - C++ - Epic Developer Community Forums) where he’s answering to someone experiencing the same problem as I am. At first, I had my functions exposed to blueprints inside a GameState class, after seeing this post I moved things to a PlayerController as suggested but no success.

I have no idea what to do anymore. Note that I can trigger this error (instead of waiting 10 - 60 seconds after connecting) simply by connecting, opening up the console and running the command OBJ gc (to force-execute garbage collection).

Please help!

0xFFFFFFFFFFFFFF… does not look like null to me, besides it passed your null check, it is invalid pointer, pointer that it is not null (it would have 0x00000… if it was) and points to invalid address or unallocated memeory

I don’t think this is FSocket that is garbage collected, as you said yourself GC can’t see what reflection system can’t see and FSocket is not support by it. So only possible culprit here is pointer containing that pointer is invalid too due to GC and UNetworkConnection is one. So my guess is you didn’t place UPROPERTY() before UNetworkConnection pointer whereever you hold it.

You may ask why function get executed then? Object functions are just a illusion same as objects, for CPU they just data structures in memory and class functions are just ordnery structural code that just operates data stracture in address dilivered by “this” pointer. If you call function from invalid pointer (which is no null it points to unused memory or some junk data) “this” pointer in object code inherents that invalid address from that pointer, if address is still pointing somewhere in allocated memory, OS will pass it thinking that there really may be object in that address and as i said function code in CPU machine code is not related to object at all, so it won’t crash on calling of function on invalid pointer (Which in you case is UNetworkPointer), it will crash on first broken pointer found, which in your case FSocket pointer which is junked after UNetworkPointer got deleted and pointing to 0xFFFFFF… which definitely is not allocated. IF you would check other varables in that class you would see they also have junk data at that point :stuck_out_tongue:

Invalid pointers are most nasty things to debug in C++, thats why you should always null your pointers wehn object get deleted and as you probably missed UPROPERTY() in UNetworkConnection it was not nulled by GC

Hey , thank you for the answer!

I tried this earlier (storing a reference to UNetworkConnection in the PlayerController class), gave it another try now, no success.

Here’s how things look:

CustomPlayerController.h:

http://puu.sh/p6Db1/f56b4e9707.png

CustomPlayerController.cpp:

http://puu.sh/p6Dg5/06789338b9.png

http://puu.sh/p6DhX/a05667bd64.png

I’m not sure what’s up with Visual Studio claiming there are errors by the way, but I added a print to ensure the code compiled - all good - I wasn’t using some old build. Tried it again and well, same results. The error happened again. :frowning:

EDIT: Errors no longer appear (was some temp visual bug I guess), but yeah, still struggling with the initial issue. :<

Hmm ok lets first be sure if UNetworkConnection is hte one get collected, type in console “displayall NetworkConnection”, this will show realtime list of all UNetworkConnection instances. Ofcorse don’t do anything with it during that so there won’t be any crashes