I have been working on unit testing small games in Gosu and have run into a segmentation fault repeatedly under similar circumstances. It seems that when I try to reuse a Gosu::Window object to test creation of either Gosu::Image or Gosu::Font objects bound to the window object, or when I try to use multiple Gosu::Window objects in the same test suite, I get a segmentation fault.
I have tried closing the window context with the #close method at the end as well and the segfault continues to occur.
Operating System: OSX Lion 10.7.2
Gosu Version: gosu (0.7.40 universal-darwin)
Ruby version: 1.9.3-p0 (using RVM and an isolated gemset with only bundler and gosu installed)
Steps to Reproduce (with output):
Single, Reused Window Context - https://gist.github.com/1433743
Multiple Window Contexts with Close - https://gist.github.com/1433771
Re: the multiple window one. It is sadly not possible to show more than once, or have multiple Window instances in Gosu. Fixing this likely requires changes on every single platform. I should have added an exception for this in the first place (until every platform is fixed).
I hope this will be fixed as a byproduct of switching to GLFW for the windowing. Patches are always welcome - until then, you should make all your tests run in the same window (same Window#show call, if any).
The one with only one Window, I don't know. It looks right, and I often create Windows without ever showing them, just for resource testing, from irb. The ivar is not being reset between test runs? Is the script process being run multiple times? (autotest sounds like one of those Guardfile things that keeps running)
minitest/autorun just tells the system to run the tests on a call to process exit, nothing that is continuously running between sections. I threw it in there to consolidate everything into one file with as little complexity as possible.
The instance variable is not being reset between test runs, I did a test outputing the object_id of the window ivar and it is consistent between test runs.
I'll also note that I have the same problem in RSpec, but I switched to minitest so I could rule rspec as the culprit, since the specs do RUN, they just segfault on cleanup.
Ah, it is implemented via at_exit. I have no idea under which circumstances at_exit blocks are run and if it is technically legal to create and destroy a Window there on OS X. At least it looks like it is run on the main thread.
I guess I'll have to try minitest myself. It's small enough to schedule a debugging session some time.
Would it be sufficient for your kind of test if the Window class was not necessary for resource creation?
I have the same issue. When I try to test Gui class then I receive segmentation fault. I've tried different RSpec versions, different Ruby versions... but with no success.
Here is my project when it fails: https://github.com/mrhead/ruby_arena/blob/master/lib/gui.rb When I comment out super in initialize then tests fail, but Ruby is not crashing...
Is there anything else what should I provide here?
@cadwallion Do you have any workaround for this? (I do not want to switch to minitest ;)
No time to look at it yet, all my side projects are exploding a little atm… :(
Not only has the Window architecture changed a lot since 2013, but Gosu::Font and other resources do not require a Window to be loaded anymore. Everything should work just fine now. Thanks again for the really detailed bug report!