Ruby C gem memory pollution between runs -


i wrote gem in c appears keeping polluted memory between runs.

i noticed in past, have discovered how reproduce consistently calling gem in rspec spring. when running single rspec example after restarting spring, example passes. subsequent runs without restarting spring result in garbage data. further subsequent runs result in same garbage data.

example:

$ spring stop spring stopped. $ spring rspec ..._spec.rb:122 ... 1 example, 0 failures  $ spring rspec ..._spec.rb:122 ... failures: ...        expected collection contained:  [7]        actual collection contained:    [1333155159] $ spring rspec ..._spec.rb:122 ... failures: ...        expected collection contained:  [7]        actual collection contained:    [1333155159]  $ spring stop spring stopped. $ spring rspec ..._spec.rb:122 ... 1 example, 0 failures  $ spring rspec ..._spec.rb:122 ... failures: ...        expected collection contained:  [7]        actual collection contained:    [117372691] $ spring rspec ..._spec.rb:122 ... failures: ...        expected collection contained:  [7]        actual collection contained:    [117372691] 

the c should not persisting between runs. global declarations constants, struct definitions, functions, , module name itself. example (simplified 1 of each type of declaration, except headers):

#include <ruby.h> #include <stdio.h> #include <stdlib.h> #include <stdbool.h>  #define some_constants 100  struct some_structs {   long id;   double amount; };  static long some_functions();  value mymodulename = qnil; value internal_function_name(value self, value rb_data, value rb_options);  void init_my_gem() {   mymodulename = rb_define_module("mymodulename");   rb_define_singleton_method(mymodulename, "exposed_function_name",     internal_function_name, 2); } 

thank ideas may have going on.

edit

i've isolated this:

printf("a orders[0].products[0]->id: %lu\n", orders[0].products[0]->id); value rb_best_orders = rb_ary_new(); printf("b orders[0].products[0]->id: %lu\n", orders[0].products[0]->id);  output: orders[0].products[0]->id: 7 b orders[0].products[0]->id: 140735021913496 

note orders defined struct order *orders;, , memory acquired via malloc.

fixed using alloc_n instead of malloc. http://clalance.blogspot.com/2011/01/writing-ruby-extensions-in-c-part-12.html helpful, though says malloc should work.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -