+1 vote

I built a dynamic library using C++ to connect through GDNative with my Godot game.

The native library compiles without any error and the game runs properly except at times when the game crashes. The Godot debugger provides no information about the crash (no error or warning), just reporting that the debugging process stopped.

Now, it could be GDScript that caused the crash, but we were likely to get information about any runtime error if it was GDScript, right? Thus, I assume that it's the native library that is causing the runtime issue.

So, how can I ensure that the native library isn't having runtime issues? Yes, I can test it separately in a C++ debugger but the problem is that it's totally dependent on the game and I can't reproduce the scenario that causes the crash when used with Godot.

Is there any way to make Godot capture the standard output/error streams of the library and log them down? C++ usually reports issues about any runtime crash, such as "segmentation fault".

Godot version 3.3.1
in Engine by (16 points)

1 Answer

+2 votes
Best answer

You should debug your shared library (dll/so) from IDE, having Godot executable as a host application and set its working directory to your project path.

Then you'll be able to use all C++'s debugging features (breakpoints, data inspectors. etc.) including all both Godot and your native-lib output in IDE's captured stdout or in terminal (if enabled).

by (1,438 points)
selected by

Thanks for your response. However, I'm confused with the following:

  • What do you mean by debugging from IDE? You mean like using the debugging features of an IDE such as Visual Studio or Visual Studio Code?
  • What do you mean by the Godot executable? Do you mean the executable of the Godot editor or my exported game?
  • What is a host application?

If you can kindly provide an example, it'd be quite helpful for me. I've been using Godot for just a couple of months at this point.

Edit: Did you mean debugging the game's process that is spawned by Godot's editor?

What do you mean by debugging from IDE? ... using the debugging features of an IDE such as Visual Studio or Visual Studio Code?

Exactly.

What do you mean by the Godot executable? What is a host application?

I'm on Linux, and didn't opened VS for a decade :-) but try this explanation: https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-debug-from-a-dll-project?view=vs-2019

Any deployable application comes in 2 major forms: standalone app (exe) and shared library (dll). The later (a gdnative case) always needs a host (calling) application to be run within its address space.

Godot application is both editor and runtime host: when you launch it with defaults (by clicking icon) it brings a project chooser. When you launch it (from console with no other options) in project's directory it will launch your game as it was exported. This is what happens when you hit "F5" in editor: it launches another Godot process and you debug it. Export template (and a final game) is the same executable with stripped down editor features.

Now back to IDE: you should set Host / Launching app / Command (terms may vary) to path/to/godot.exe and Working directory to path/to/dir/of/your/project.godot. The later my be specified in commandline. See godot --help. Then press "debug" in IDE and it will launch your game with ability to debug your code.

This is exactly what I needed. I was totally in the dark about the way Godot can be used from the terminal. I can't thank you enough!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.