Runaway memory in extension

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 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.