Rb_raise with dynamic strings and GC

Hi All,

An abstract C snippet to detail my question:

char *message;

message = returns_error_message_that_was_malloced();

errstr = rb_str_new2(message);
free(message);

rb_raise(eMyError, “%s”, RSTRING(errstr)->ptr);

What will happen with errstr after the function returns since I did not
really use it as part of an object?
Will errstr be found by the GC and cleaned up?

I could just use message with rb_raise, but I would rather not have a
malloc being done far away and the free responsibility to be after use
in the rb_raise. Is this a bad idea? Should I just use
rb_exc_raise(errstr)?

I love Ruby and the C extension constructs are great and clean (better
than Python and Tcl from my experience).

All the best,

Dan

···


Dan Janowski
danj@3skel.com

Hi,

What will happen with errstr after the function returns since I did not
really use it as part of an object?
Will errstr be found by the GC and cleaned up?

Yes, it is expected so.

I could just use message with rb_raise, but I would rather not have a
malloc being done far away and the free responsibility to be after use
in the rb_raise. Is this a bad idea? Should I just use
rb_exc_raise(errstr)?

Not bad. Or,

errobj = rb_exc_new2(eMyError, message);
free(message);
rb_exc_raise(errobj);

I guess you know that rb_exc_raise() expects an Exception

instance, and it was just a typo.

···

At Sat, 2 Aug 2003 09:12:44 +0900, Dan Janowski wrote:


Nobu Nakada