crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.47k stars 1.62k forks source link

Adding an Array(Int32) field to a class breaks --release build #7012

Closed ghost closed 5 years ago

ghost commented 6 years ago

Crystal 0.26.1 (2018-09-27) LLVM: 6.0.1 Default target: x86_64-pc-linux-gnu

I am running Antergos (Arch Linux).

I have the following code:

require "sdl"

class Grid
  @rand = Random.new

  @grid : Array(Bool)
  @temp : Array(Bool)
  # @intensity : Array(Int32)

  def initialize(@grid_width : Int32, @grid_height : Int32)
    @grid = Array(Bool).new(@grid_width * @grid_height, false)
    @temp = Array(Bool).new(@grid.size, false)
    # @intensity = Array(Int32).new(@grid.size, 0)
    randomize
  end

  def randomize
    (0...@grid.size).each do |i|
      @grid[i] = @rand.next_bool
    end
  end
end

SDL.init(SDL::Init::VIDEO)
at_exit { SDL.quit }

window = SDL::Window.new("Life", 1280, 720)
renderer = SDL::Renderer.new(window, SDL::Renderer::Flags::ACCELERATED | SDL::Renderer::Flags::PRESENTVSYNC)
width, height = window.size

grid = Grid.new width, height

loop do
  case event = SDL::Event.poll
  when SDL::Event::Quit
    break
  when SDL::Event::Keyboard
    break
  end

  renderer.present
end

Here's my shard.yml:

name: life.cr
version: 0.1.0

authors:
  - Scitoshi Nakayobro <matthewtpeterson1@gmail.com>

targets:
  kitty_clicker:
    main: src/life.cr

dependencies:
    sdl:
        github: ysbaddaden/sdl.cr

crystal: 0.26.1

license: MIT

If I uncomment the two commented lines, the program compiles and 'runs', but the window never opens. This only happens on a release build; without --release, it works fine. I haven't been able to get any kind of error out of it thus far.

jkthorne commented 6 years ago

This might be a SDL bug. Have you tried adding logging to see if everything is still running?

ghost commented 6 years ago

The main loop is running, based on logging. What I can't understand is how this would be an SDL bug. I agree that it seems quite plausible given that the loop is still running, however, as far as I can tell based on the code, SDL and the Grid class have no interaction with each other, so I can't understand why adding that field would cause the window not to open.

asterite commented 6 years ago

The SDL bindings could be implemented in a wrong way. Mostly likely the struct's sizes are not the correct ones and so the stack memory is being overrun by something else and that causes the segmentation fault.

ghost commented 6 years ago

@asterite What segmentation fault? The program runs... it's just that the window doesn't open...

ghost commented 6 years ago

Just made an interesting discovery: putting a puts statement in the main loop (or above it) ALSO prevents the window from opening...

asterite commented 6 years ago

Ah, sorry, well, just incorrect behavior then.

asterite commented 6 years ago

I would open this issue in the SDL project/shard.

ghost commented 6 years ago

So it appears that the solution to this was to call window.update. The fact that the code works as is without --release makes me think that this is a compiler bug of some kind. Whether it be the Crystal compiler itself or an issue with LLVM... to me, the behavior should not change between debug and release builds.

jhass commented 5 years ago

Still reproduces using Crystal 0.29.0-dev [397bfe6e9]

vlazar commented 5 years ago

Works now. I see an empty black UI window when I run program.

Crystal 0.31.1 on MacOS ``` $ crystal --version Crystal 0.31.1 (2019-10-02) LLVM: 8.0.1 Default target: x86_64-apple-macosx ```
sdl2: stable 2.0.10 ``` $ brew info sdl2 sdl2: stable 2.0.10 (bottled), HEAD Low-level access to audio, keyboard, mouse, joystick, and graphics https://www.libsdl.org/ /usr/local/Cellar/sdl2/2.0.10 (87 files, 4.6MB) * Poured from bottle on 2019-10-14 at 12:41:31 From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/sdl2.rb ==> Options --HEAD Install HEAD version ==> Analytics install: 67,239 (30 days), 193,676 (90 days), 789,026 (365 days) install_on_request: 7,547 (30 days), 20,950 (90 days), 85,555 (365 days) build_error: 0 (30 days) ```
asterite commented 5 years ago

Let's close this. It's. probably related to SDL bindings.