# SystemStackError when rb\_define\_module\_function() \* 3

**URL:** <https://www.ruby-forum.com/t/systemstackerror-when-rb-define-module-function-3/69696>\
**Category:** Ruby\
**Created:** [February 17, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/systemstackerror-when-rb-define-module-function-3/69696 "2006-02-17T17:14:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [February 17, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/systemstackerror-when-rb-define-module-function-3/69696/1 "2006-02-17T17:14:42Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Hello,

I’m getting a “test.rb:46: stack level too deep (SystemStackError)”  
when defining three or more functions for a module, which is created  
& given to Ruby via the Ruby C API. The test.rb (actually  
“trunk/src/samp/test.rb”) file and the remaining source code is  
available in a Subversion repository here:

svn checkout -r21 svn://rubyforge.org/var/svn/ruby-vpi

- -or-  
[http://rubyforge.org/plugins/scmsvn/viewcvs.php/?root=ruby-vpi&pathrev=21](http://rubyforge.org/plugins/scmsvn/viewcvs.php/?root=ruby-vpi&pathrev=21)

In particular, the error occurs when I register a third module  
function with Ruby C API inside line 25 of the file  
“trunk/src/RVPI.cin”:

// register the VPI module  
RVPI\_\_rModuleDef = rb\_define\_module(“VPI”);

rb\_define\_module\_function( // first func  
RVPI\_\_rModuleDef  
, “relay\_verilog”  
, RVPI\_rb\_relay\_verilog  
, 0  
);

rb\_define\_module\_function( // second func  
RVPI\_\_rModuleDef  
, “register\_task”  
, RVPI\_rb\_register\_task  
, 1  
);

rb\_define\_module\_function( // third func  
RVPI\_\_rModuleDef  
, “handle\_by\_name”  
, RVPI\_rb\_handle\_by\_name  
, 2  
); // FIXME: causes “stack level too deep (SystemStackError)”

I originally thought the problem was with the third module function  
(named “RVPI\_rb\_handle\_by\_name”) that I was trying to register. But  
this was not the case, because the SystemStackError occurred even  
when I registered the first module function under a different name:

// register the VPI module  
RVPI\_\_rModuleDef = rb\_define\_module(“VPI”);

rb\_define\_module\_function( // first func  
RVPI\_\_rModuleDef  
, “relay\_verilog”  
, RVPI\_rb\_relay\_verilog  
, 0  
);

rb\_define\_module\_function( // second func  
RVPI\_\_rModuleDef  
, “register\_task”  
, RVPI\_rb\_register\_task  
, 1  
);

rb\_define\_module\_function( // first func with different name  
RVPI\_\_rModuleDef  
, “foo”  
, RVPI\_rb\_relay\_verilog  
, 0  
);

What am I doing wrong?

Thank you.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.1 (GNU/Linux)

iD4DBQFD9ULQmV9O7RYnKMcRAnzuAKCvyNqkSWWM3o7kpK1l2y8Xup98hACWOrGI  
qDsBxZ9L9CfILnHqlsLnaQ==  
=1Ilp  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [February 19, 2006, 1:55pm UTC](https://www.ruby-forum.com/t/systemstackerror-when-rb-define-module-function-3/69696/2 "2006-02-19T13:55:04Z")

</div>

Hi,

In message “Re: SystemStackError when rb\_define\_module\_function() \* 3”  
on Sat, 18 Feb 2006 01:13:29 +0900, Suraj K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|In particular, the error occurs when I register a third module  
|function with Ruby C API inside line 25 of the file  
|“trunk/src/RVPI.cin”:

Did SystemStackError happened during rb\_define\_module\_function()?  
What OS are you using?

There’s no explicit limitation for number of rb\_define\_module\_function()  
calls, except for OS resource limit. SystemStackError is caused when  
call stack is too deep, and the max depth is calculated in Init\_stack().  
Since you are using embedded Ruby, you may fail to call Init\_stack()  
in the interpreter initialization.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [February 19, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/systemstackerror-when-rb-define-module-function-3/69696/3 "2006-02-19T20:19:08Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Yukihiro M. wrote:

> Did SystemStackError happened during rb\_define\_module\_function()?

No, it happened later, when the Ruby interpreter was executing a script.

> What OS are you using?

Linux 2.6.12-10-386 #1 Mon Jan 16 17:18:08 UTC 2006 i686 GNU/Linux

> Since you are using embedded Ruby, you may fail to call Init\_stack()  
> in the interpreter initialization.

Ah, thank you. I tried putting a call to Init\_stack() in my  
initialization code but no luck. However, the problem is neither  
with embedded Ruby nor with my initialization of the Ruby interpreter:

After some more investigation I happened upon the answer. There was  
indeed a heap/stack limitation upon the Ruby interpreter embedded in  
my C program!

(my C program, inside its own process  
… unlimited stack space …

(Ruby interpreter, inside a pthread  
… 2 MiB of stack space …

)  
)

I was using the pthread library, with default attribute values  
(thread stack size = 2 MiB) for pthread\_create(), to launch the Ruby  
interpreter. Naturally, the interpreter eventually ran out of stack  
space and produced the SystemStackError.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFD+MOcmV9O7RYnKMcRAleEAJ4yC3X0OFEoEolyEsv1YNObj0MDBgCfSiJA  
sVF/m8hSVv9ZhEmlzwxamVE=  
=bVQ+  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [February 24, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/systemstackerror-when-rb-define-module-function-3/69696/4 "2006-02-24T18:46:18Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Sorry, I forgot to specify the version of Ruby I am using:

$ ruby --version  
ruby 1.8.3 (2005-06-23) [i486-linux]  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFD9UUbmV9O7RYnKMcRAku+AJ0fT6XGjoth4xxtUrzThHTTsCkhCwCeM6qT  
JdFhC9WTrGH9/iVtj9VBVOY=  
=jgf0  
-----END PGP SIGNATURE-----
