runaway memory in extension

J

Jeff Mitchell

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/
 
J

Joel VanderWerf

Jeff said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top