0

Edit: This leak does not happen on Linux, so perhaps this is a MacOS Big Sur issue?

Building the following code with the following flags produces a Direct leak. Is this a false positive or an actual leak in SDL Net?

Flags:

-g -O0 -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize=vptr

Code:

if (SDL_Init(SDL_INIT_EVENTS) < 0)
{
    std::cerr << "Failed to init SDL." << std::endl;
    return;
}

if (SDLNet_Init() < 0)
{
    std::cerr << "Failed to init SDL Net." << std::endl;
    return;
}

auto port = 8080;

IPaddress localServerIP;
if (SDLNet_ResolveHost(&localServerIP, nullptr, port) < 0)
{
    std::cerr << "Failed to resolve the TCP server host:" << SDLNet_GetError() << std::endl;
    return;
}

// Direct leak here...
auto tcpServerSocket = SDLNet_TCP_Open(&localServerIP);
if (!tcpServerSocket)
{
    std::cerr << "Failed to open the TCP server socket: " << SDLNet_GetError() << std::endl;
    return;
}

This is the detected leak error:

===========================================================
==71118==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 28 byte(s) in 1 object(s) allocated from:
    #0 0x10acc12b0 in wrap_malloc (/usr/local/opt/llvm/lib/clang/11.1.0/lib/darwin/libclang_rt.asan_osx_dynamic.dylib:x86_64+0x462b0)
    #1 0x10abe9c47 in SDL_DYNAPI_entry (/Library/Frameworks/SDL2.framework/Versions/A/SDL2:x86_64+0xdac47)
    #2 0x107c7df27 in SDLNet_TCP_Open (app:x86_64+0x10059cf27)
    #3 0x1079e7b54 in Run(int, char**) (app:x86_64+0x100306b54)
    #4 0x1076e46cd in main (app:x86_64+0x1000036cd)
    #5 0x7fff20667620  (/usr/lib/system/libdyld.dylib:x86_64+0x15620)

    SUMMARY: AddressSanitizer: 28 byte(s) leaked in 1 allocation(s).
rhughes
  • 9,257
  • 11
  • 59
  • 87
  • 2
    Are you calling `SDLNet_TCP_Close` on `tcpServerSocket` when you're finished with it? – Woodford Apr 16 '21 at 23:36
  • It doesn't get that far - the program crashes when the leak is detected. – rhughes Apr 16 '21 at 23:38
  • 2
    If you're experiencing a crash, it seems highly unlikely that a memory leak could be the cause. You might try tracking that down first and dealing with the leak later. – Woodford Apr 16 '21 at 23:44
  • The crash is because I'm using `-fsanitize=address` - so the crash is correct. I would like to know why the leak is being detected, as it seems it is happening within SDL itself. – rhughes Apr 16 '21 at 23:50
  • 1
    It sounds very unlikely leak checker is able to detect leak immediately; most probably checking&reporting happens when your program completes (basically report everyting that was allocated but not freed, that's quite simple task actually), not immediately when symbol goes out of scope, and my simple test confirms that. I'm fairly sure your program crashes before it calls `SDLNet_TCP_Close` (or not actually crashes but still never call that) - that's a leak on your side. Run in debugger and check your test code line by line; edit in MCCVE if you can't find the problem. – keltar Apr 17 '21 at 05:06

1 Answers1

0

Testing on Linux, it looks like there is no leak. This may be an issue with MacOS Big Sur itself, or something unrelated that happened to be reported as a leak when using SDLNet.

rhughes
  • 9,257
  • 11
  • 59
  • 87