Search code examples
crubyruby-c-extension

Is memory freed after using StringValueCStr?


I use StringValueCStr to convert ruby String to char*. Do i need to free memory when this new C string is no longer needed?

If this new C string is freed automatically, does this mean i should copy it if i plan on saving it in my C structures for later use?


Solution

  • StringValueCStr returns the same pointer that StringValuePtr does -- which is the internal string buffer of the object.

    That buffer could change or be invalidated by any subsequent mutation of the String instance (or, of course, a garbage collection), so yes you should copy it if you need to keep it around.