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.

···

--
Iñaki Baz Castillo <ibc@aliax.net>

Hi,

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.

···

In message "Re: C extension: "malloc" and "rb_raise"" on Sat, 24 Oct 2009 08:43:32 +0900, Iñaki Baz Castillo <ibc@aliax.net> writes:

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

Regards.

···

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

Hi,

In message "Re: C extension: "malloc" and "rb_raise"" > > on Sat, 24 Oct 2009 08:43:32 +0900, Iñaki Baz Castillo <ibc@aliax.net> 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.

--
Iñaki Baz Castillo <ibc@aliax.net>

Hi,

···

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

El Sábado, 24 de Octubre de 2009, Yukihiro Matsumoto escribió:
> In message "Re: C extension: "malloc" and "rb_raise"" > > on Sat, 24 Oct 2009 08:43:32 +0900, Iñaki Baz Castillo <ibc@aliax.net> 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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
Still I'm convinced there should be a String#notempty? method.

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.

···

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

Hi,

Am Samstag, 24. Okt 2009, 08:55:11 +0900 schrieb Iñaki Baz Castillo:
> El Sábado, 24 de Octubre de 2009, Yukihiro Matsumoto escribió:
> > In message "Re: C extension: "malloc" and "rb_raise"" > > > > > > on Sat, 24 Oct 2009 08:43:32 +0900, Iñaki Baz Castillo <ibc@aliax.net> 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.

--
Iñaki Baz Castillo <ibc@aliax.net>

Iñaki Baz Castillo 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 Sonntag, 25. Okt 2009, 00:32:43 +0900 schrieb Iñaki Baz Castillo:

El Sábado, 24 de Octubre de 2009, Bertram Scharpf 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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Hi,

···

In message "Re: C extension: "malloc" and "rb_raise"" on Sun, 25 Oct 2009 02:06:42 +0900, Tim Hunter <TimHunter@nc.rr.com> 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.

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.

···

El Domingo, 25 de Octubre de 2009, Bertram Scharpf escribió:

Hi,

Am Sonntag, 25. Okt 2009, 00:32:43 +0900 schrieb Iñaki Baz Castillo:
> El Sábado, 24 de Octubre de 2009, Bertram Scharpf 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.

--
Iñaki Baz Castillo <ibc@aliax.net>