# Is using Libc.free fine when a library doesn't have its own freeing function?

**URL:** <https://discourse.julialang.org/t/is-using-libc-free-fine-when-a-library-doesnt-have-its-own-freeing-function/55326>\
**Category:** General Usage\
**Tags:** ccall, memory\
**Created:** [February 15, 2021, 3:53pm UTC](https://discourse.julialang.org/t/is-using-libc-free-fine-when-a-library-doesnt-have-its-own-freeing-function/55326 "2021-02-15T15:53:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MasFlam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masflam/32/20950_2.png) [@MasFlam](https://discourse.julialang.org/u/MasFlam)\
**Post date:** [February 15, 2021, 3:53pm UTC](https://discourse.julialang.org/t/is-using-libc-free-fine-when-a-library-doesnt-have-its-own-freeing-function/55326/1 "2021-02-15T15:53:06Z")

</div>

The XCB library occasionally requires the user to free data by themselves using the libc `free` function. The documentation on `Libc.free` in Julia specifically says it shouldn’t be used to free data from external libraries and to use the memory freeing functions provided by the libraries used. The problem is, as I stated above, XCB does not provide such function. Can I then use `Libc.free` for freeing memory it allocates for me, or do I have to live with a memleak?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [February 15, 2021, 4:26pm UTC](https://discourse.julialang.org/t/is-using-libc-free-fine-when-a-library-doesnt-have-its-own-freeing-function/55326/2 "2021-02-15T16:26:55Z")

</div>

I believe you have to. There doesn’t appear to be any technical reason memory allocated by one library can’t be deallocated by another. It’s just a very frowned upon interface.

And apparently on Windows it can cause issues.

[https://stackoverflow.com/questions/36420174/linux-c-c-allocate-deallocate-memory-in-dynamic-library](https://stackoverflow.com/questions/36420174/linux-c-c-allocate-deallocate-memory-in-dynamic-library)

---

<div class="post-metadata">

**Author:** ![MasFlam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masflam/32/20950_2.png) [@MasFlam](https://discourse.julialang.org/u/MasFlam)\
**Post date:** [February 15, 2021, 4:47pm UTC](https://discourse.julialang.org/t/is-using-libc-free-fine-when-a-library-doesnt-have-its-own-freeing-function/55326/3 "2021-02-15T16:47:10Z")

</div>

Well, X11 users are most probably on posix platforms so I don’t think I have to worry about that, although wrapping the frees in `if`s checking the platform might be a good idea…
