Handmade Hero»Forums»Code
2 posts
When using musl, can you free() a pointer that was allocated by an external library that was linked with glibc?
Edited by tuket on

Hi, I was wondering if this would work correctly:

  • I'm using the xcb library from my program for making windows. Some functions in xcb return pointers that you have to free() yourself. I'm assuming that xcb uses glibc for calling malloc().
  • Now I would like my program to statically link musl libc. But I'm guessing that the malloc/free functions are not compatible, right?

What can be done about this?

Thanks!

Mārtiņš Možeiko
2357 posts / 2 projects
When using musl, can you free() a pointer that was allocated by an external library that was linked with glibc?
Edited by Mārtiņš Možeiko on

You must use free() function from same libc instance that was used to allocate it.

If you use xcb library then there's no reason to avoid libc - as it requires you to use its libc instance to free() pointers. You need to link to same libc dynamically - which can be glibc or musl depending on distro.

Symbol resolving on Linux works a bit differently from Windows. Loading multiple libc instances in same process is really not supported and most likely won't work. You need to use libraries that are compiled with same libc implementation & loads same instance into your process. Otherwise you'll have strange errors and have really bad time.

2 posts
When using musl, can you free() a pointer that was allocated by an external library that was linked with glibc?
Replying to mmozeiko (#25672)

Thanks mate, that makes a lot of sense.