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

[iOS] System.Diagnostics.Debug.Assert causing app to crash #113874

Closed
i-nikityuk opened this issue Mar 25, 2025 · 3 comments
Closed

[iOS] System.Diagnostics.Debug.Assert causing app to crash #113874

i-nikityuk opened this issue Mar 25, 2025 · 3 comments

Comments

@i-nikityuk
Copy link

Apple platform

iOS

Framework version

net8.0-*

Affected platform version

VS For Mac 17.6.14 / Rider 2024.3.6

Description

Reality:
Call Debug.Assert(false, "Message text"); from AppDelegate or any other place in the iOS app (mine is NET8, NOT MAUI) - app will crash (if debugger is not attached)

Expectations:
app will continue execution without a crash

Steps to Reproduce

  1. Create new empty iOS app
  2. call Debug.Assert(false, "Message text"); from any place
  3. Run app without debugger attached
  4. app will crash
  5. if debugger is attached app will continue execution with console output similar to this:

---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
Message text
---- Assert Long Message ----

at System.Diagnostics.DefaultTraceListener.Fail(String message, String detailMessage)
at System.Diagnostics.TraceInternal.Fail(String message, String detailMessage)
at System.Diagnostics.TraceInternal.TraceProvider.Fail(String message, String detailMessage)
at System.Diagnostics.Debug.Fail(String message, String detailMessage)
at System.Diagnostics.Debug.Assert(Boolean condition, String message, String detailMessage)
at System.Diagnostics.Debug.Assert(Boolean condition, String message)
....... removed.
NOTE: this is not a crash ^^ - it is just output of "new StackTrace(bool fNeedFileInfo).ToString()" called internally in "DefaultTraceListener.Fail"

Did you find any workaround?

WORKAROUND:
As Debug.Assert using "DefaultTraceListener" by default, setting "AssertUiEnabled" property to false fixed the issue.

DefaultTraceListener traceListener = new DefaultTraceListener();
traceListener.AssertUiEnabled = false;

Trace.Listeners.Clear();
Trace.Listeners.Add(traceListener);

Relevant logs

Issue looks identical or at least very close to this issue, despite being on another platform
#40512

@rolfbjarne
Copy link
Member

This behavior comes from here:

if (Debugger.IsAttached)
{
Debugger.Break();
}
else
{
// In Core, we do not show a dialog.
// Fail in order to avoid anyone catching an exception and masking
// an assert failure.
DebugAssertException ex = new DebugAssertException(message, detailMessage, stackTrace);
Environment.FailFast(ex.Message, ex, errorSource);
}

so it looks intentional.

That said, what would you expect the behavior to be?

In any case, moving to dotnet/runtime, since that's where the code in question resides.

@rolfbjarne rolfbjarne transferred this issue from dotnet/macios Mar 25, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 25, 2025
@filipnavara
Copy link
Member

That sounds intentional to me, it's the normal behavior on desktop platforms too. The asserts are compiled-out in Release builds.

@i-nikityuk
Copy link
Author

Thank you for your response guys.

My expectations were:

In Release builds - asserts are compiled-out. (all good here)

In Debug builds:

if (Debugger.IsAttached)
{
// Exactly the behaviour I would expect, stop debugger on Assert call
Debugger.Break(); // All good here
}
else
{
// However, here I would expect just console output without
// terminating the app
}

=====================================================

After reading comments in @rolfbjarne response ("Fail in order to avoid anyone catching an exception and masking an assert failure.") I think it all make sense and just my assumptions/expectations of Debug.Assert were incorrect.

Thanks once again, close it.

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants