PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Do strings from rb_str_new and friends need to be free'd?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Do strings from rb_str_new and friends need to be free'd?

Réponse
 
LinkBack Outils de la discussion
Vieux 10/05/2008, 04h21   #1
Steven Kah Hien Wong
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 11/05/2008, 06h14   #2
Steven Kah Hien Wong
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 <matz@ruby-lang.org>
wrote:

> 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" <
> steven@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.
>
>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h17.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,07884 seconds with 10 queries