Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertion callbacks #100

Merged
merged 11 commits into from
Dec 26, 2021
Merged

Assertion callbacks #100

merged 11 commits into from
Dec 26, 2021

Conversation

tlf30
Copy link
Contributor

@tlf30 tlf30 commented Dec 25, 2021

This PR implements the ability to receive a callback in java from the native layer upon an IM_ASSERT(_EXPR) call.
The default callback which is registered by ImGui upon initialization will print the assertion, file and line number, along with the stack trace to pinpoint the call within the user application code that cause the assertion failure.
Example:
image
Here you can see the call to popStyleColor caused the assertion failure.

The default callback can be overridden by the user by calling:

ImGui.setAssertCallback(new ImAssertCallback() {
            @Override
            public void imAssertCallback(String assertion, int line, String file) {
                //Some user code
            }
        });

The default Dear ImGui assertion behavior can be restored by setting the callback to null:

ImGui.setAssertCallback(null);

Some details about the implementation:
Due to how ImGui expects execution to end upon an assertion failure, it is not capable of continuing execution after a failed assertion. As such, this implementation will call a System.exit(1) after the user callback has completed. It is not possible to throw an exception to terminate execution as the native call must complete before the exception propagates up the stack to become uncaught and terminate the application. In the event that the callback throws an exception, it is caught and printed with a warning, along with some basic information about the assertion.
Please see the javadocs for more information.

Please let me know what you think.
Thank you,
Trevor

@SpaiR SpaiR added the feat New feature or request label Dec 25, 2021
Copy link
Owner

@SpaiR SpaiR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I've left some minor comments. Please address them and the PR will be ready for a merge.

imgui-binding/src/main/java/imgui/ImGui.java Outdated Show resolved Hide resolved
imgui-binding/src/main/native/jni_assertion.cpp Outdated Show resolved Hide resolved
@SpaiR
Copy link
Owner

SpaiR commented Dec 26, 2021

LGTM! As always, great job!

@SpaiR SpaiR merged commit 74203bc into SpaiR:master Dec 26, 2021
@SpaiR SpaiR linked an issue Dec 26, 2021 that may be closed by this pull request
@serivesmejia
Copy link
Contributor

Thank you sooooo much for the addition! You have encouraged me to continue working on a project and saved me so many future debugging headaches lol 🙏 very appreciated

@tlf30
Copy link
Contributor Author

tlf30 commented Dec 27, 2021

No problem @serivesmejia, this was something that I believe will help many people trying to debug issues, including myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Debugging "assertion failed"
3 participants