# Does \`String(unsafe\_wrap(cstring, len, own=true))\` free the memory it points to?

**URL:** <https://discourse.julialang.org/t/does-string-unsafe-wrap-cstring-len-own-true-free-the-memory-it-points-to/136315>\
**Category:** General Usage\
**Tags:** memory-allocation, c\
**Created:** [March 20, 2026, 11:13pm UTC](https://discourse.julialang.org/t/does-string-unsafe-wrap-cstring-len-own-true-free-the-memory-it-points-to/136315 "2026-03-20T23:13:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [March 20, 2026, 11:13pm UTC](https://discourse.julialang.org/t/does-string-unsafe-wrap-cstring-len-own-true-free-the-memory-it-points-to/136315/1 "2026-03-20T23:13:35Z")

</div>

If I have a `Cstring` from a `@ccall` that I am responsible for freeing, does converting that `Cstring` to a Julia `String` via `s = String(unsafe_wrap(Vector{UInt8}, Ptr{UInt8}(cstring), len, own=true))` automatically free the associated memory once `s` goes out of scope?

---

<div class="post-metadata">

**Author:** ![obsidianjulua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/obsidianjulua/32/221495_2.png) [@obsidianjulua](https://discourse.julialang.org/u/obsidianjulua)\
**Post date:** [March 21, 2026, 1:27am UTC](https://discourse.julialang.org/t/does-string-unsafe-wrap-cstring-len-own-true-free-the-memory-it-points-to/136315/2 "2026-03-21T01:27:55Z")

</div>

unsafe\_string() (copy) is almost always what you want. The unsafe\_wrap(own=true) pattern is only safe when you’re certain the pointer came from malloc and ownership transfers to you, but yes if know malloc was used and not a custom allocator.

---

<div class="post-metadata">

**Author:** ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)\
**Post date:** [March 21, 2026, 1:47am UTC](https://discourse.julialang.org/t/does-string-unsafe-wrap-cstring-len-own-true-free-the-memory-it-points-to/136315/3 "2026-03-21T01:47:02Z")

</div>

The `String` constructor might make a copy anyway. If you want a copy-free wrap see [GitHub - JuliaStrings/StringViews.jl: String-like views of arbitrary Julia byte arrays · GitHub](https://github.com/JuliaStrings/StringViews.jl) this can be used with a custom array type with a finalizer to call the C functions to clean up the memory.
