nbnet icon indicating copy to clipboard operation
nbnet copied to clipboard

Soak test: Segfault when calling deinit functions

Open nathhB opened this issue 3 years ago • 3 comments

On all tested platforms except for OSX, there is a segfault when calling NBN_GameClient_Deinit or NBN_GameServer_Deinit. For now, it was only reproduced with the soak test but I believe it's a library issue.

nathhB avatar Oct 09 '21 13:10 nathhB

running echo_bytes client without starting a server (on linux); valgrind complains:

==112049== Invalid read of size 8
==112049==    at 0x10BD1E: NBN_Connection_Destroy (nbnet.h:2414)
==112049==    by 0x10F4F6: NBN_GameClient_Deinit (nbnet.h:3872)
==112049==    by 0x115333: main (client.c:211)
==112049==  Address 0x4de16d8 is 1,106,200 bytes inside a block of size 1,106,768 free'd
==112049==    at 0x48399AB: free (vg_replace_malloc.c:538)
==112049==    by 0x1094B0: MemPool_Deinit (nbnet.h:1305)
==112049==    by 0x1093BB: MemoryManager_Deinit (nbnet.h:1262)
==112049==    by 0x10EC11: NBN_Endpoint_Deinit (nbnet.h:3617)
==112049==    by 0x10F4DB: NBN_GameClient_Deinit (nbnet.h:3869)
==112049==    by 0x115333: main (client.c:211)
==112049==  Block was alloc'd at
==112049==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==112049==    by 0x10960F: MemPool_Grow (nbnet.h:1344)
==112049==    by 0x10947C: MemPool_Init (nbnet.h:1299)
==112049==    by 0x109390: MemoryManager_Init (nbnet.h:1249)
==112049==    by 0x10E8F1: NBN_Endpoint_Init (nbnet.h:3522)
==112049==    by 0x10F4B2: NBN_GameClient_Init (nbnet.h:3861)
==112049==    by 0x1151E7: main (client.c:120)

calling NBN_Connection_Destroy before NBN_Endpoint_Deinit in NBN_GameClient_Deinit fixes it

btw there are some memory leaks:

==112173== 6,144 bytes in 3 blocks are definitely lost in loss record 2 of 3
==112173==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==112173==    by 0x10C597: NBN_Connection_CreateChannel (nbnet.h:2626)
==112173==    by 0x10ED76: NBN_Endpoint_CreateConnection (nbnet.h:3653)
==112173==    by 0x10FB14: NBN_GameClient_CreateServerConnection (nbnet.h:4083)
==112173==    by 0x114DBA: NBN_Driver_GCli_Start (udp.h:388)
==112173==    by 0x10F54A: NBN_GameClient_Start (nbnet.h:3878)
==112173==    by 0x1151EC: main (client.c:122)
==112173== 
==112173== 1,106,768 bytes in 1 blocks are definitely lost in loss record 3 of 3
==112173==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==112173==    by 0x114D3A: NBN_Driver_GCli_Start (udp.h:371)
==112173==    by 0x10F54A: NBN_GameClient_Start (nbnet.h:3878)
==112173==    by 0x1151EC: main (client.c:122)

undernorthernsky avatar Oct 31 '21 15:10 undernorthernsky

@undernorthernsky thank you for the investigation. Actually, it makes a lot of sense because of this line: https://github.com/nathhB/nbnet/blob/master/nbnet.h#L3617

Trying to destroy anything after destroying the memory manager will obviously raise issues.

Regarding the memory leaks, I'll look into it!

nathhB avatar Nov 01 '21 11:11 nathhB

This commit fixes the segfault: https://github.com/nathhB/nbnet/commit/7ef4d42cd32e6f45c6b991a4d50a7b7e3b4b0cb0

I'll close the issue when the memory leaks are fixed.

nathhB avatar Nov 02 '21 08:11 nathhB