Google Groups Home
Help | Sign in
Do strings from rb_str_new and friends need to be free'd?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Steven Kah Hien Wong  
View profile
 More options May 9, 11:21 pm
Newsgroups: comp.lang.ruby
From: Steven Kah Hien Wong <ste...@zensaki.com>
Date: Fri, 9 May 2008 22:21:57 -0500
Local: Fri, May 9 2008 11:21 pm
Subject: Do strings from rb_str_new and friends need to be free'd?
[Note:  parts of this message were removed to make it a legal post.]

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Yukihiro Matsumoto  
View profile
 More options May 9, 11:36 pm
Newsgroups: comp.lang.ruby
From: Yukihiro Matsumoto <m...@ruby-lang.org>
Date: Fri, 9 May 2008 22:36:04 -0500
Local: Fri, May 9 2008 11:36 pm
Subject: Re: Do strings from rb_str_new and friends need to be free'd?
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" <ste...@zensaki.com> 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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Kah Hien Wong  
View profile
 More options May 11, 1:14 am
Newsgroups: comp.lang.ruby
From: Steven Kah Hien Wong <ste...@zensaki.com>
Date: Sun, 11 May 2008 00:14:30 -0500
Local: Sun, May 11 2008 1:14 am
Subject: Re: Do strings from rb_str_new and friends need to be free'd?
[Note:  parts of this message were removed to make it a legal post.]

Ah, cool. Thanks, Matz!

-Steven

On Sat, May 10, 2008 at 1:36 PM, Yukihiro Matsumoto <m...@ruby-lang.org>
wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google