Forum: Ruby rb_raise problem in C extension

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8812d2385b477f8fbbe37f22c40b18c5?d=identicon&s=25 unknown (Guest)
on 2009-04-22 19:07
(Received via mailing list)
I am using Ruby 1.8.6-26 from the One Click Installer on Windows. I
have a C extension that tries to calloc() memory. If the calloc()
fails I call rb_raise(rb_eNoMemError,"Cannot allocate data"). My
program is getting stuck in this code. Debugging (unfortunately via
print statements) I can see that rb_raise() is going to be called.
After that the exception is never caught by the outermost rescue loop.
The program just stops doing anything (0% cpu) except it keeps
updating a timer in another thread. Are there things I need to know
about rb_raise() and how to use it?
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2009-04-22 20:46
(Received via mailing list)
On Apr 22, 2009, at 10:05, bdezonia@wisc.edu wrote:
> I am using Ruby 1.8.6-26 from the One Click Installer on Windows. I
> have a C extension that tries to calloc() memory. If the calloc()
> fails I call rb_raise(rb_eNoMemError,"Cannot allocate data"). My
> program is getting stuck in this code. Debugging (unfortunately via
> print statements) I can see that rb_raise() is going to be called.
> After that the exception is never caught by the outermost rescue loop.
> The program just stops doing anything (0% cpu) except it keeps
> updating a timer in another thread. Are there things I need to know
> about rb_raise() and how to use it?

If ruby is out of memory how could it allocate more memory to raise an
exception?

Ruby itself allocates a NoMemError at startup to ensure it can raise
one when it runs out of memory.  You'll probably need to do the same.
See gc.c rb_memerror().
8812d2385b477f8fbbe37f22c40b18c5?d=identicon&s=25 unknown (Guest)
on 2009-04-22 21:56
(Received via mailing list)
On Apr 22, 1:44 pm, Eric Hodel <drbr...@segment7.net> wrote:
> > about rb_raise() and how to use it?
>
> If ruby is out of memory how could it allocate more memory to raise an  
> exception?
>
> Ruby itself allocates a NoMemError at startup to ensure it can raise  
> one when it runs out of memory.  You'll probably need to do the same.  
> See gc.c rb_memerror().

There is plenty of memory available (4-6 gig free). But I'm asking
calloc() for a 1 gb chunk and it can't find one. Is there a different
exception I should throw in this case? Will the rb_raise() in my
nested C code percolate out to my handler in my nested ruby code? (As
a test for now I'm just changing it to an eException but would
appreciate any feedback you have)
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2009-04-23 00:04
(Received via mailing list)
On Apr 22, 2009, at 12:55, bdezonia@wisc.edu wrote:
>>> updating a timer in another thread. Are there things I need to know
> There is plenty of memory available (4-6 gig free). But I'm asking
> calloc() for a 1 gb chunk and it can't find one. Is there a different
> exception I should throw in this case? Will the rb_raise() in my
> nested C code percolate out to my handler in my nested ruby code? (As
> a test for now I'm just changing it to an eException but would
> appreciate any feedback you have)

In that case, rb_raise should do what you want, however you may need
to explicitly rescue it:

$ ruby
begin
   begin
     raise NoMemoryError
   rescue
     puts "caught with plain rescue"
   end
rescue Exception # or NoMemoryError
   puts "caught with rescue Exception"
end
^D
caught with rescue Exception


For this reason, you should use RuntimeError or StandardError
(especially for custom error classes) instead of Exception, since
Exception isn't caught by a plain rescue.
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2009-04-23 19:41
(Received via mailing list)
On Wed, 22 Apr 2009 13:44:52 -0500, Eric Hodel wrote:

>
> If ruby is out of memory how could it allocate more memory to raise an
> exception?
>
> Ruby itself allocates a NoMemError at startup to ensure it can raise one
> when it runs out of memory.  You'll probably need to do the same. See
> gc.c rb_memerror().

Is rb_memerror() exposed for him to call? He could just call that, and
it
would spare him all issues with preallocation.

--Ken
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2009-04-23 19:50
(Received via mailing list)
On Apr 23, 2009, at 10:40, Ken Bloom wrote:
>>> the exception is never caught by the outermost rescue loop. The
>> Ruby itself allocates a NoMemError at startup to ensure it can
>> raise one
>> when it runs out of memory.  You'll probably need to do the same. See
>> gc.c rb_memerror().
>
> Is rb_memerror() exposed for him to call? He could just call that,
> and it
> would spare him all issues with preallocation.

yeah, looks like it's in intern.h.
This topic is locked and can not be replied to.