C extension: "malloc" and "rb_raise"

Hi, if I use “malloc” in a Ruby C function and then call to “rb_raise”
(so the
program exists), will the allocated memory be released? (if not I get a
memory
leak).
Perhaps Ruby havs a garbage collector for C?

Thanks.

Hi,

In message “Re: C extension: “malloc” and “rb_raise””
on Sat, 24 Oct 2009 08:43:32 +0900, Iñaki Baz C. [email protected]
writes:

|Hi, if I use “malloc” in a Ruby C function and then call to “rb_raise” (so the
|program exists), will the allocated memory be released? (if not I get a memory
|leak).

No.

|Perhaps Ruby havs a garbage collector for C?

Ruby GC only handles Ruby objects and memory regions pointed by them.

          matz.

El Sábado, 24 de Octubre de 2009, Yukihiro M.
escribió:> Hi,

In message “Re: C extension: “malloc” and “rb_raise””

on Sat, 24 Oct 2009 08:43:32 +0900, Iñaki Baz C. <[email protected]> 

writes:

|Hi, if I use “malloc” in a Ruby C function and then call to “rb_raise” (so
| the program exists), will the allocated memory be released? (if not I get
| a memory leak).

No.

|Perhaps Ruby havs a garbage collector for C?

Ruby GC only handles Ruby objects and memory regions pointed by them.

Thanks for clarify it. So I must use free() before rb_raise().

Regards.

Iñaki Baz C. wrote:

It’s a small piece of C code, no more. Do you mean that I shouldn’t use
“malloc”/“free” in a Ruby C extension?

Of course you can. Just be prepared to free the memory before raising an
exception. Also check out Ruby’s xmalloc and xfree. These functions act
like malloc and free but the memory will be garbage collected when Ruby
determines that there are no more references to it.

Hi,

Am Samstag, 24. Okt 2009, 08:55:11 +0900 schrieb Iñaki Baz C.:

El Sábado, 24 de Octubre de 2009, Yukihiro M. escribió:

In message “Re: C extension: “malloc” and “rb_raise””
on Sat, 24 Oct 2009 08:43:32 +0900, Iñaki Baz C. [email protected] writes:

|Perhaps Ruby havs a garbage collector for C?

Ruby GC only handles Ruby objects and memory regions pointed by them.

Thanks for clarify it. So I must use free() before rb_raise().

It’s surely a matter of taste but I think you should not. Mixing
different allocation methods in one C source file will detain you
from understanding your own code just one month later.

Bertram

El Sábado, 24 de Octubre de 2009, Bertram S.
escribió:> Hi,

Am Samstag, 24. Okt 2009, 08:55:11 +0900 schrieb Iñaki Baz C.:

El Sábado, 24 de Octubre de 2009, Yukihiro M. escribió:

In message “Re: C extension: “malloc” and “rb_raise””

on Sat, 24 Oct 2009 08:43:32 +0900, Iñaki Baz C. 

[email protected] writes:

|Perhaps Ruby havs a garbage collector for C?

Ruby GC only handles Ruby objects and memory regions pointed by them.

Thanks for clarify it. So I must use free() before rb_raise().

It’s surely a matter of taste but I think you should not. Mixing
different allocation methods in one C source file will detain you
from understanding your own code just one month later.

It’s a small piece of C code, no more. Do you mean that I shouldn’t use
“malloc”/“free” in a Ruby C extension?

Regards.

Hi,

In message “Re: C extension: “malloc” and “rb_raise””
on Sun, 25 Oct 2009 02:06:42 +0900, Tim H. [email protected]
writes:

|Also check out Ruby’s xmalloc and xfree. These functions act
|like malloc and free but the memory will be garbage collected when Ruby
|determines that there are no more references to it.

Unfortunately, regions allocated by xmalloc() will not be garbage
collected. You still need to deallocate them by xfree(). The
difference is that garbage collector will be kicked when a) underlying
malloc() failed, b) certain amount of memory region is allocated, to
make more free space.

          matz.

Hi,

Am Sonntag, 25. Okt 2009, 00:32:43 +0900 schrieb Iñaki Baz C.:

El Sábado, 24 de Octubre de 2009, Bertram S. escribió:

It’s a small piece of C code, no more. Do you mean that I shouldn’t use
“malloc”/“free” in a Ruby C extension?

README.EXT recommends to allocate space by the ALLOC() macro which
calls Ruby’s xmalloc(). I cannot find an xfree() call there; I
admit I do not understand that.

Bertram

El Domingo, 25 de Octubre de 2009, Bertram S.
escribió:> Hi,

Am Sonntag, 25. Okt 2009, 00:32:43 +0900 schrieb Iñaki Baz C.:

El Sábado, 24 de Octubre de 2009, Bertram S. escribió:

It’s a small piece of C code, no more. Do you mean that I shouldn’t use
“malloc”/“free” in a Ruby C extension?

README.EXT recommends to allocate space by the ALLOC() macro which
calls Ruby’s xmalloc(). I cannot find an xfree() call there; I
admit I do not understand that.

Note that I use “malloc” jsut for pure C usage, this is, I don’t
allocate
memory for Ruby objects and so…

Anyhow, I’ve read this interesting mail (from 2002 however) in which the
author recommends to use “malloc/free” rather tahn ALLOW:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/47669

Regards.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs