Papierkorb / qt5.cr

Qt5 bindings for Crystal, based on Bindgen
Mozilla Public License 2.0
211 stars 20 forks source link

Segmentation fault when exiting the built HelloWorld example #12

Closed loloof64 closed 4 years ago

loloof64 commented 6 years ago

Hello, first congratulation for such a work and for the simplicity of your README. (The only drawback is that you don't mention the need to install the libgc-dev package).

I've built the HelloWorld sample on LinuxMint 18.3 64bit, but when exiting the application (reproduced after several compilation-launch), I get a segmentation fault. I don't know if this come from Crystal or from the qt binding

$ ./hello_world 
Invalid memory access (signal 11) at address 0x7f4459776889
[0x5aa796] *CallStack::print_backtrace:Int32 +118
[0x59f66b] __crystal_sigfault_handler +75

And again, great work.

Papierkorb commented 6 years ago

Hi,

  1. What hello world sample do you mean? The one in samples/ or the one in the README?
  2. Which Qt version are you on? You can either ask APT or qmake-qt5 --version.
  3. Are you using the ready-to-use branch, or have you generated the bindings yourself?
  4. Does a program work fine which just creates a Qt::Application and then exits?
loloof64 commented 6 years ago

Hello,

  1. The hello world sample is the one in the samples subdirectory
  2. I've been installing the Qt framework with the command sudo apt-get install qtbase5-dev (and the libqt5opengl5-dev package also)
  3. I am using the master-ready-to-use branch
  4. I've been testing the following program, but I had to do CTRL+C in order to stop it, so as for now I can't give you a precise answer

The program which I've been tested just with Qt::Application.exec()

require "qt5"

# Create the application first
qApp = Qt::Application.new

# And now, start it!
Qt::Application.exec

Regards

Papierkorb commented 6 years ago

That behaviour is expected, as you're starting the event loop with the call to .exec. This kicks off a Qt application and will only return after it has been exited, which usually also ends the process. Just remove that line and retry.

Also, I need the Qt version that is installed, not how you installed it.

refi64 commented 6 years ago

For future reference, the version of Qt 5 in Mint 18.3/Ubuntu Xenial is 5.5.1.

EDIT: Oh wow, I timed that comment perfectly. 👌

Papierkorb commented 6 years ago

@kirbyfan64 So kinda old, but the oldest version there are shipped bindings for, so that's fine. Thanks

loloof64 commented 6 years ago

Ok : this works perfectly

require "qt5"

# Create the application first
qApp = Qt::Application.new

# And now, start it!
Papierkorb commented 6 years ago

Could it be that the order the finalizers are called is 'wrong'?

Try running the hello world sample without the last five lines:

# We're ready for showtime
window.show

# And now, start it!
Qt::Application.exec

I hope it still crashes?

loloof64 commented 6 years ago

Did you mean the following ?

require "qt5"

# Create the application first
qApp = Qt::Application.new

# We'll use a normal widget as window
window = Qt::Widget.new
window.window_title = "Hello, World!"

# We need to give it a layout
layout = Qt::VBoxLayout.new
window.layout = layout

# Create a label and a button, and push it into the layout
button = Qt::PushButton.new "Click me!"
label = Qt::Label.new "Click the button!"

layout << button << label

# On every press on `button`, we want to change the label.
counter = 0

button.on_pressed do # This is how you connect to the `pressed` signal
  counter += 1
  label.text = "You pressed #{counter} times!"
end

I've just tried this code and it returns immediately without crashing (but without showing any window).

Also I build the executable with the command crystal build hello_world.cr. Maybe should I have used shard for this.

Papierkorb commented 6 years ago

Yes that's what I meant. Kinda sucks that it doesn't crash though. Back to square 1.

You'd have to figure out what object lies at the address given in the log in your opening post. You can do that using gdb, maybe valgrind could also help to show what's going on, etc.. But that's nothing I could give you easy directions for in terms of this issue tracker.

I assume it has something to do with the garbage collector fighting with Qt under certain circumstances. One might delete an object before the other tries to use it or deletes it too. These kind of things are time consuming to figure out (My assumption might be totally wrong!), and probably tedious to fix afterwards as well.

Sorry to bring bad news on this.

loloof64 commented 6 years ago

Unfortunately I don't know how to use gdb nor valgrind. Anyway thank you very much for your patience and for having trying to help me.

Regards

refi64 commented 6 years ago

@loloof64 Try building your application in debug mode and then running valgrind path-to-my-app. Valgrind is a memory checker; it'll print out any potential wrong memory accesses.

loloof64 commented 6 years ago

Thank you :)

$ valgrind ./hello_world 2> errors

Produced me the errors file I've attached errors.zip

Otherwise

$ crystal run -d ./hello_world
Syntax error in hello_world:1: unknown token: '\u{7f}'

ELF>�Y@�lX@8
^
docelic commented 4 years ago

This may have been solved by a temporary workaround that now exists in bindgen (added in commit https://github.com/Papierkorb/bindgen/commit/01265f9c6c00df89a7bc26c1faf5b11df6c60bd8 and tracked for further refinement in https://github.com/Papierkorb/bindgen/issues/37)

docelic commented 4 years ago

I am going to close this ticket since in addition to the previous comment I have also tested myself and can't reproduce the issue. Please reopen or submit a new issue if the problem persists.