# Rb\_raise problem in C extension

**URL:** <https://www.ruby-forum.com/t/rb-raise-problem-in-c-extension/166206>\
**Category:** Ruby\
**Created:** [April 22, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/rb-raise-problem-in-c-extension/166206 "2009-04-22T19:07:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 22, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/rb-raise-problem-in-c-extension/166206/1 "2009-04-22T19:07:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 22, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/rb-raise-problem-in-c-extension/166206/2 "2009-04-22T20:46:29Z")

</div>

On Apr 22, 2009, at 10:05, removed\_email\_address@domain.invalid 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().

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 22, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/rb-raise-problem-in-c-extension/166206/3 "2009-04-22T21:56:05Z")

</div>

On Apr 22, 1:44 pm, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 23, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/rb-raise-problem-in-c-extension/166206/4 "2009-04-23T19:41:01Z")

</div>

On Wed, 22 Apr 2009 13:44:52 -0500, Eric H. 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 23, 2009, 12:04am UTC](https://www.ruby-forum.com/t/rb-raise-problem-in-c-extension/166206/5 "2009-04-23T00:04:33Z")

</div>

On Apr 22, 2009, at 12:55, removed\_email\_address@domain.invalid 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 23, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/rb-raise-problem-in-c-extension/166206/6 "2009-04-23T19:50:23Z")

</div>

On Apr 23, 2009, at 10:40, Ken B. 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.
