runaway memory in extension

Discussion in 'Ruby' started by Jeff Mitchell, May 19, 2004.

  1. Can someone tell me why memory grows without bound:

    arda:~/ex> cat extconf.rb
    require 'mkmf'
    create_makefile('myclass')
    arda:~/ex> cat myclass.c

    #include "ruby.h"

    struct MyStruct
    {
    int n ;
    } ;

    void myclass_free(struct MyStruct* data)
    {
    }

    VALUE rb_myclass_initialize( VALUE self )
    {
    struct MyStruct *data;
    Data_Get_Struct(self, struct MyStruct, data);
    return Qnil ;
    }

    static VALUE rb_myclass_s_allocate(VALUE klass)
    {
    struct MyStruct* data ;
    VALUE obj = Data_Make_Struct(klass,
    struct MyStruct,
    0,
    myclass_free,
    data) ;
    return obj ;
    }

    VALUE cMyClass ;

    void Init_myclass()
    {
    cMyClass = rb_define_class("MyClass", rb_cObject) ;
    rb_define_alloc_func(cMyClass, rb_myclass_s_allocate) ;
    rb_define_method(cMyClass, "initialize", rb_myclass_initialize, 0) ;
    }

    arda:~/ex> cat test.rb
    require './myclass'
    loop { MyClass.new }
    arda:~/ex> ruby test.rb





    __________________________________
    Do you Yahoo!?
    SBC Yahoo! - Internet access at a great low price.
    http://promo.yahoo.com/sbc/
     
    Jeff Mitchell, May 19, 2004
    #1
    1. Advertising

  2. Jeff Mitchell wrote:
    > Can someone tell me why memory grows without bound:

    ...
    > void myclass_free(struct MyStruct* data)
    > {

    free(data);
    > }


    It's the responsibility of your code to free the struct. See 3.3 of
    README.EXT.
     
    Joel VanderWerf, May 19, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vtcompsci

    runaway button

    vtcompsci, Feb 23, 2006, in forum: Java
    Replies:
    4
    Views:
    716
    vtcompsci
    Feb 24, 2006
  2. Anand
    Replies:
    3
    Views:
    919
    Tim Daneliuk
    Nov 8, 2003
  3. Follower
    Replies:
    0
    Views:
    343
    Follower
    May 7, 2004
  4. Martin DeMello

    runaway cgi process

    Martin DeMello, Sep 2, 2004, in forum: Ruby
    Replies:
    0
    Views:
    147
    Martin DeMello
    Sep 2, 2004
  5. Replies:
    4
    Views:
    251
Loading...

Share This Page