# Do strings from rb\_str\_new and friends need to be free'd?

**URL:** <https://www.ruby-forum.com/t/do-strings-from-rb-str-new-and-friends-need-to-be-freed/137212>\
**Category:** Ruby\
**Created:** [May 10, 2008, 5:22am UTC](https://www.ruby-forum.com/t/do-strings-from-rb-str-new-and-friends-need-to-be-freed/137212 "2008-05-10T05:22:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven\_Kah\_Hien\_Won](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@Steven\_Kah\_Hien\_Won](https://www.ruby-forum.com/u/Steven_Kah_Hien_Won)\
**Post date:** [May 10, 2008, 5:22am UTC](https://www.ruby-forum.com/t/do-strings-from-rb-str-new-and-friends-need-to-be-freed/137212/1 "2008-05-10T05:22:46Z")

</div>

Hi,

I have some C native code that calls a logging class written in Ruby. To  
pass the message string to the Ruby class, I am creating a string with  
rb\_str\_new2 and then passing its VALUE to rb\_funcall. I am wondering if  
I  
need to do any clean-up for the string, or will Ruby handle this for me?

Also, if anyone can suggest an awesome doc on writing Ruby native  
extensions, that would be appreciated. At the moment, I am mostly using  
the  
Pickaxe online book.

Here is the code in question:

```
void log_warning(const char *fmt, ...)
{
    char message[BUFFER_LEN]; // Temporary buffer to hold the 

```

generated  
message

```
    // Evaluate the format string and store it in the temporary 

```

buffer  
va\_list ap;  
va\_start(ap, fmt);  
vsnprintf(message, BUFFER\_LEN, fmt, ap);  
va\_end(ap);

```
    // Find the ID's to call tho Log.warning Ruby class method
    rb_require("log");
    ID log_class = rb_path2class("Log");
    ID log_warning_method = rb_intern("warning");

    // Create a Ruby string containing a copy of the formatted 

```

message  
VALUE message\_value = rb\_str\_new2(message);

```
    // Call Log.warning, with the formatted message as an argument
    rb_funcall(log_class, log_warning_method, 1, message_value);

    // Do I need to do anything here to tell Ruby to clean up the

```

string?  
}

Thanks!

-Steven

---

<div class="post-metadata">

**Author:** ![Steven\_Kah\_Hien\_Won](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@Steven\_Kah\_Hien\_Won](https://www.ruby-forum.com/u/Steven_Kah_Hien_Won)\
**Post date:** [May 11, 2008, 7:15am UTC](https://www.ruby-forum.com/t/do-strings-from-rb-str-new-and-friends-need-to-be-freed/137212/2 "2008-05-11T07:15:05Z")

</div>

Ah, cool. Thanks, Matz!

-Steven

On Sat, May 10, 2008 at 1:36 PM, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Steven\_Kah\_Hien\_Won](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@Steven\_Kah\_Hien\_Won](https://www.ruby-forum.com/u/Steven_Kah_Hien_Won)\
**Post date:** [May 10, 2008, 5:37am UTC](https://www.ruby-forum.com/t/do-strings-from-rb-str-new-and-friends-need-to-be-freed/137212/3 "2008-05-10T05:37:57Z")

</div>

Hi,

In message “Re: Do strings from rb\_str\_new and friends need to be  
free’d?”  
on Sat, 10 May 2008 12:21:57 +0900, “Steven Kah Hien Wong”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|I have some C native code that calls a logging class written in Ruby. To  
|pass the message string to the Ruby class, I am creating a string with  
|rb\_str\_new2 and then passing its VALUE to rb\_funcall. I am wondering if I  
|need to do any clean-up for the string, or will Ruby handle this for me?

The latter. You don’t have to (and you shouldn’t) free string  
objects.

```
          matz.

```
