ssue with Custom TLSF Allocator in SDL3 Exiting with Code 42 on SDL_Quit()


I am currently working on a project where I am integrating a custom TLSF memory allocator into SDL3. I have connected this custom allocator to SDL’s memory functions using SDL_SetMemoryFunctions(). Everything seems to be working fine, except for when I call SDL_Quit().

Even when reduced to running code that just connects my allocator, sets it up, and then runs SDL_Init() and SDL_Quit(), it exits with code 42 after the SDL_Quit() call. Following GDB I have found that the issue seems to be a assert within the mutex file that fails. However, given that TLSF isn’t a thread safe memory allocator, I wrapped all my memory allocation functions within locked SDL_Mutex’s but I still regain the same error. And really struggle to imagine how my allocator might not be thread-safe.

Here is pretty much the wrapper that all my memory functions are wrapped in.

void *sdl_tlsf_malloc(size_t bytes) {


	SDL_Log("Malloc Called on %ld bytes\n", bytes);

	// Check if we have enough memory to allocate

	void *ptr = tlsf_malloc(active_instance.instance, bytes);

	// Update metadata structs

	return ptr;

Has anyone encountered a similar issue or does anyone have any insights on what might be causing this? Any help would be greatly appreciated. Maybe I’m using mutex’s wrong and missing something? I haven’t worked with locks and mutex’s since my System Progamming course so I’m a bit rusty and could see myself making assumptions about how they work.

Thank you in advance!

This isn’t going to work because SDL calls the allocator when it creates a mutex, which means that it will have memory that isn’t tracked by your allocator, and will crash when it’s freed with your deallocator, unless you do some fancy tap dancing.

You need to set the memory allocation functions as the very first API call you make, otherwise you’ll see crashes as you describe as there will be mismatched calls to allocate and free memory.

OH! Of course! Thank you! I can’t believe I didn’t think of that, I’ll probably have to reconfigure things a little bit then! Thank You!