rb_raise with dynamic strings and GC

D

Dan Janowski

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
 
N

nobu.nokada

Hi,

At Sat, 2 Aug 2003 09:12:44 +0900,
Dan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,351
Latest member
RoxiePulli

Latest Threads

Top