# SWIG / Ruby GC issues

**URL:** <https://www.ruby-forum.com/t/swig-ruby-gc-issues/118528>\
**Category:** Ruby\
**Created:** [October 28, 2007, 5:31am UTC](https://www.ruby-forum.com/t/swig-ruby-gc-issues/118528 "2007-10-28T05:31:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![redroofgreentree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@redroofgreentree](https://www.ruby-forum.com/u/redroofgreentree)\
**Post date:** [October 28, 2007, 5:31am UTC](https://www.ruby-forum.com/t/swig-ruby-gc-issues/118528/1 "2007-10-28T05:31:48Z")

</div>

Hi all,

I have this really nasty SWIG/ruby GC issue. (Namely objects are being  
freed when I don’t want them to be freed).

These bugs are hard to track down, here’s what would really help me:

Is there some way I can tell ruby that when it frees an object, to  
ZERO  
out that entire region in memory? It’ll make tracking down the bug so  
much  
easier.

Thanks!

---

<div class="post-metadata">

**Author:** ![redroofgreentree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@redroofgreentree](https://www.ruby-forum.com/u/redroofgreentree)\
**Post date:** [October 28, 2007, 6:23am UTC](https://www.ruby-forum.com/t/swig-ruby-gc-issues/118528/2 "2007-10-28T06:23:11Z")

</div>

redroofgreentree wrote:

> Thanks!

Well … I guess I would start off by saying, “gdb is your friend”. 🙂  
But all SWIG does is generate wrapper/interface code between the Ruby  
and the C or C++ code. Can you make this work without SWIG?

---

<div class="post-metadata">

**Author:** ![redroofgreentree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@redroofgreentree](https://www.ruby-forum.com/u/redroofgreentree)\
**Post date:** [October 28, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/swig-ruby-gc-issues/118528/3 "2007-10-28T16:06:20Z")

</div>

M. Edward (Ed) Borasky wrote:

> redroofgreentree wrote:
> 
> > I have this really nasty SWIG/ruby GC issue. (Namely objects are being  
> > freed when I don’t want them to be freed).
> 
> Well … I guess I would start off by saying, “gdb is your friend”. 🙂  
> But all SWIG does is generate wrapper/interface code between the Ruby  
> and the C or C++ code. Can you make this work without SWIG?

SWIG does help with memory management,; there’s an unusually helpful  
section on GC in the SWIG docs, which repays close reading  
[http://www.swig.org/Doc1.3/Ruby.html#Ruby\_nn57](http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn57)

You definitely need a debugger to identify what’s getting prematurely  
destroyed, but once you have a stack trace which shows what method is  
being called on a deleted object, it’s normally not hard to write a  
%markfunc to protect it

alex

---

<div class="post-metadata">

**Author:** ![redroofgreentree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@redroofgreentree](https://www.ruby-forum.com/u/redroofgreentree)\
**Post date:** [October 29, 2007, 6:13pm UTC](https://www.ruby-forum.com/t/swig-ruby-gc-issues/118528/4 "2007-10-29T18:13:08Z")

</div>

On Sun, Oct 28, 2007 at 01:31:10PM +0900, redroofgreentree wrote:

> I have this really nasty SWIG/ruby GC issue. (Namely objects are being  
> freed when I don’t want them to be freed).
> 
> These bugs are hard to track down, here’s what would really help me:
> 
> Is there some way I can tell ruby that when it frees an object, to ZERO  
> out that entire region in memory? It’ll make tracking down the bug so much  
> easier.

Valgrind often helps in tracking down these sorts of issues. You’ll  
need a suppressions file; the one I use is located at:

[http://rubystuff.org/ruby.supp](http://rubystuff.org/ruby.supp)

Paul
