# \[bug\] serious memory leak + continuations

**URL:** <https://rubytalk.org/t/bug-serious-memory-leak-continuations/15418>\
**Category:** ruby-talk\
**Created:** [21 December 2004 13:33 UTC](https://rubytalk.org/t/bug-serious-memory-leak-continuations/15418 "2004-12-21T13:33:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![George5](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@George5](https://rubytalk.org/u/George5)\
**Post date:** [21 December 2004 13:33 UTC](https://rubytalk.org/t/bug-serious-memory-leak-continuations/15418/1 "2004-12-21T13:33:47Z")

</div>

Hi,

ruby 1.9.0 (2004-12-20) [i386-freebsd5.2.1]  
gcc version 3.3.3 [FreeBSD] 20031106

In my application, if I change:

```
# (1) this works
callback_stream.with_callbacks_for(self, :action) { |callback, val|
   catch(:something) { callback.call }
   throw :wee_back_to_session
 }

```

into:

```
# (2) this leaks
callback_stream.with_callbacks_for(self, :action) { |callback, val|
   res = catch(:something) { callback.call }
   throw :wee_back_to_session
 }

```

or into:

```
 # (3) this leaks
 callback_stream.with_callbacks_for(self, :action) { |callback, val|
   callback.call
   throw :wee_back_to_session
 }

```

memory consumption is unbounded! BTW, “:something” is never thrown. It  
seems that if I reference the return value of callback.call, the memory  
leak appears. The leak also happens if I change “res = " in (2) into  
”@res = " or "$res = ". Note that inside callback.call continuations are  
created.

And if I return “nil” from the block (callback.call), it leaks, too,  
even if I use code sample (1).

I tried above example also with ruby-stable compiled with 2.95.4 (-O0  
enabled), but there all three examples leak memory.

It seems to be related to the “unknown node type 0 bug”, as it appears  
to be pretty random (at least in my eyes):

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/123694](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/123694)

Help! I can send the whole sources to someone who is interested. Only  
requirements are rubygems, installed narf gem (webunit) and gnuplot to  
display memory consumption, and of course \*nix.

Regards,

```
Michael
```

---

<div class="post-metadata">

**Author:** ![George5](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@George5](https://rubytalk.org/u/George5)\
**Post date:** [21 December 2004 17:45 UTC](https://rubytalk.org/t/bug-serious-memory-leak-continuations/15418/2 "2004-12-21T17:45:29Z")

</div>

Michael Neumann wrote:

> Hi,
> 
> ruby 1.9.0 (2004-12-20) [i386-freebsd5.2.1]  
> gcc version 3.3.3 [FreeBSD] 20031106
> 
> In my application, if I change:
> 
> # (1) this works
> 
> callback\_stream.with\_callbacks\_for(self, :action) { |callback, val|  
> catch(:something) { callback.call }  
> throw :wee\_back\_to\_session  
> }
> 
> into:
> 
> # (2) this leaks
> 
> callback\_stream.with\_callbacks\_for(self, :action) { |callback, val|  
> res = catch(:something) { callback.call }  
> throw :wee\_back\_to\_session  
> }
> 
> or into:
> 
> ```
> # (3) this leaks
> callback_stream.with_callbacks_for(self, :action) { |callback, val|
> callback.call
> throw :wee_back_to_session
> }
> 
> ```
> 
> memory consumption is unbounded! BTW, “:something” is never thrown. It  
> seems that if I reference the return value of callback.call, the memory  
> leak appears. The leak also happens if I change "res = " in (2) into  
> "@res = " or "$res = ". Note that inside callback.call continuations are  
> created.
> 
> And if I return “nil” from the block (callback.call), it leaks, too,  
> even if I use code sample (1).
> 
> I tried above example also with ruby-stable compiled with 2.95.4 (-O0  
> enabled), but there all three examples leak memory.
> 
> It seems to be related to the “unknown node type 0 bug”, as it appears  
> to be pretty random (at least in my eyes):
> 
> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/123694](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/123694)
> 
> Help! I can send the whole sources to someone who is interested. Only  
> requirements are rubygems, installed narf gem (webunit) and gnuplot to  
> display memory consumption, and of course \*nix.

The memory leak probably results from the fact that the continuations  
are not garbage collected. The number of them keep growing in (2) and  
(3), whereas in (1) it stays constant.

Regards,

Michael
