-1

I have a stack overflow problem I can't seem to solve in a ruby project. In the file movie.rb, at the line 44-47, I call a function contained in the file mmc.rb.
When I test the file alone, mmc.rb works fine, but called several time in a loop, it causes a SO.

My stacktrace is in the following Gist.

I really don't understand why I got this.

Simon
  • 619
  • 2
  • 9
  • 23

1 Answers1

1

This is not a definitive answer but just 2 remarks:

First, there is no need or benefit for defining methods/functions within functions, your code just becomes unnecessarily complicated. Try to avoid this pattern if you can work around it (and in your case there is absolutely no need to have code like this).

Second: Your Stack Level Too Deep happens somewhere within the Net::HTTP module, so I quickly checked your code regarding Net::HTTP calls. You are using the open function from the open-uri library but you are not using it properly: whenever you open some stream for reading, you have to close it when you are done. You can achieve that using the following technique:

# sequential version -> you close the stream by yourself when
# you are done with it
stream = open('http://some.url/some/path')
data = stream.read
stream.close

# block version -> the open function closes the stream for you
# as soon as the passed block is evaluated.
data = open('http://some.url/some/path') {|stream| stream.read}

So you should replace all occurrences of open(...).read in your code with open(...) {|s| s.read}... This might as well fixes your stack level too deep, but I can't guarantee it. But as soon as you have refactored the code it will be easier to debug your problem...

severin
  • 10,148
  • 1
  • 39
  • 40