From: Eero Saynatkari Date: 2005-11-08T18:43:13+09:00 Subject: Re: crash of extension on rb_str_new2 Eero Saynatkari wrote: > newbie wrote: > >> Hi, all >> I am integrating a 3rd party API with ruby extension. The code is >> roughly like this >> void my_function() >> { >> char p[256]; >> ...//assign the value of p in the api >> >> return rb_str_new2(p);//This cause a crash. >> } >> while if I change the last part a little bit as, >> VALUE str=rb_str_new2(p); >> return str; >> This seems not crashing >> >> It looks like more related to ruby GC? Can anyone explain on this? and >> how to prevent this kind of crash? > > > p is a pointer to a local array which may be causing > the problem since the memory is released when you exit > the function's scope. You should probably be malloc()ing > the original string to char* p instead just in general > C programming terms. > > That said, I am not quite sure why the second version > would work if that were the underlying cause here. Bah, too little C lately. The function's return type should be VALUE, not void (if you have this in your actual code). Warning about the local array still stands :) >> Thanks! E