# Possible memory bug in Ruby? I'm stumped!

**URL:** <https://rubytalk.org/t/possible-memory-bug-in-ruby-im-stumped/167>\
**Category:** ruby-talk\
**Created:** [9 June 2002 07:08 UTC](https://rubytalk.org/t/possible-memory-bug-in-ruby-im-stumped/167 "2002-06-09T07:08:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kurt\_Euler1](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@Kurt\_Euler1](https://rubytalk.org/u/Kurt_Euler1)\
**Post date:** [9 June 2002 07:08 UTC](https://rubytalk.org/t/possible-memory-bug-in-ruby-im-stumped/167/1 "2002-06-09T07:08:47Z")

</div>

All-

The code as written after the dashed line way below (with file name t7.rb) correctly inserts the contents of file “intro\_subtemplate.txt” into a template file during each iteration, resulting in the creation of some 189 identical but differently named files in directory “./output”. (There are 189  
filenames in the 189 rows in file “packages.txt”.)

However, if I uncomment the two lines starting at “insertb”, only 171 files are created, then the program stops with the following message:

t7.rb:6:in ‘initialize’: Invalid argument - “./input/intro\_subtemplate.txt” (Errno::EINVAL)  
from t7.rb:6:in 'new’  
from t7.rb:6  
from t7.rb:2in 'foreach’  
from t7.rb:2

Interestingly, If I then uncomment the two lines beginning at “insertc”, only 128 files are created before the program stops with the a similar message but specifying a different code line from the just-uncommented lines:

t7.rb:15:in ‘initialize’: Invalid argument - “./output/SOL\_BroadbandManager\_6.2\_SP1” (Errno::EINVAL)  
from t7.rb:15:in 'new’  
from t7.rb:15  
from t7.rb:2in 'foreach’  
from t7.rb:2

Is this the result of some type of memory leak or improper programming? (Note: I’ll ultimatelly have some 6 or 8 files that need to be inserted into each copy of the “main\_readme\_template” at the position of various tags in this template.) Please help, I’m stumped!

Thanks in advance!!!

-Kurt Euler  
(I’m using Ruby 1.67)

File t7.rb:

> **···**
>
> * * *
> 
> content = File.new("./input/main\_readme\_template.txt").read  
> IO.foreach("./input/packages.txt") { |x|  
> field = x.chop.split(’:’)  
> content2 = content
> 
> inserta = File.new("./input/intro\_subtemplate.txt").read  
> content2.gsub!(/\<intro\_subtemplate.txt\>/, inserta)
> 
> #insertb = File.new("./input/install\_unix\_subtemplate.txt").read  
> #content2.gsub!(/\<install\_unix\_subtemplate.txt\>/, insertb)
> 
> #insertc = File.new("./input/files\_changed\_sp\_num-1\_subtemplate.txt").read  
> #content2.gsub!(/\<files\_changed\_sp\_num-1\_subtemplate.txt"\>/, insertc)
> 
> File.new("./output/"+field[0],“w+”).write(content2)  
> puts field[0]  
> }

---

<div class="post-metadata">

**Author:** ![Park\_Heesob5](https://avatars.discourse-cdn.com/v4/letter/p/77aa72/32.png) [@Park\_Heesob5](https://rubytalk.org/u/Park_Heesob5)\
**Post date:** [9 June 2002 08:38 UTC](https://rubytalk.org/t/possible-memory-bug-in-ruby-im-stumped/167/2 "2002-06-09T08:38:11Z")

</div>

Hi,

“Kurt Euler” [keuler@portal.com](mailto:keuler@portal.com) wrote in message  
[news:C47CCC6238EFD4119C5200508B95A100074AF63B@cup1ex1.portal.com](mailto:news:C47CCC6238EFD4119C5200508B95A100074AF63B@cup1ex1.portal.com)…

> All-
> 
> The code as written after the dashed line way below (with file name t7.rb)  
> correctly inserts the contents of file “intro\_subtemplate.txt” into a  
> template file during each iteration, resulting in the creation of some 189  
> identical but differently named files in directory “./output”. (There are  
> 189  
> filenames in the 189 rows in file “packages.txt”.)
> 
> t7.rb:6:in ‘initialize’: Invalid argument -  
> “./input/intro\_subtemplate.txt” (Errno::EINVAL)  
> from t7.rb:6:in ‘new’  
> from t7.rb:6  
> from t7.rb:2in ‘foreach’  
> from t7.rb:2
> 
> t7.rb:15:in ‘initialize’: Invalid argument -  
> “./output/SOL\_BroadbandManager\_6.2\_SP1” (Errno::EINVAL)  
> from t7.rb:15:in ‘new’  
> from t7.rb:15  
> from t7.rb:2in ‘foreach’  
> from t7.rb:2

These error messages caused by too many open files.

> Thanks in advance!!!
> 
> -Kurt Euler  
> (I’m using Ruby 1.67)
> 
> ## File t7.rb:
> 
> content = File.new(“./input/main\_readme\_template.txt”).read  
> IO.foreach(“./input/packages.txt”) { |x|  
> field = x.chop.split(‘:’)  
> content2 = content
> 
> inserta = File.new(“./input/intro\_subtemplate.txt”).read  
> content2.gsub!(/\<intro\_subtemplate.txt\>/, inserta)
> 
> #insertb = File.new(“./input/install\_unix\_subtemplate.txt”).read  
> #content2.gsub!(/\<install\_unix\_subtemplate.txt\>/, insertb)
> 
> #insertc =  
> File.new(“./input/files\_changed\_sp\_num-1\_subtemplate.txt”).read  
> #content2.gsub!(/\<files\_changed\_sp\_num-1\_subtemplate.txt"\>/, insertc)
> 
> File.new(“./output/”+field[0],“w+”).write(content2)  
> puts field[0]  
> }

Following three lines should be placed before IO.foeach loop.

inserta = File.new(“./input/intro\_subtemplate.txt”).read  
insertb = File.new(“./input/install\_unix\_subtemplate.txt”).read  
insertc = File.new(“./input/files\_changed\_sp\_num-1\_subtemplate.txt”).read

Or

inserta = File.new(“./input/intro\_subtemplate.txt”).read

should be

f = File.new(“./input/intro\_subtemplate.txt”)  
inserta = f.read  
f.close

Park Heesob.

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Dell](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@Sean\_O\_Dell](https://rubytalk.org/u/Sean_O_Dell)\
**Post date:** [9 June 2002 16:38 UTC](https://rubytalk.org/t/possible-memory-bug-in-ruby-im-stumped/167/3 "2002-06-09T16:38:31Z")

</div>

Park Heesob wrote:

> “Kurt Euler” [keuler@portal.com](mailto:keuler@portal.com) wrote in message  
> [news:C47CCC6238EFD4119C5200508B95A100074AF63B@cup1ex1.portal.com](mailto:news:C47CCC6238EFD4119C5200508B95A100074AF63B@cup1ex1.portal.com)…
> 
> > t7.rb:6:in ‘initialize’: Invalid argument -
> 
> These error messages caused by too many open files.

This is exactly the sort of problem that GC causes. Those files should  
completely destroyed each time they go out of scope during the iteration.

```
Sean

```
