I'm using a library which attempts to do Debug.Assert when I do a certion thing, is there anyway to stop it from using Debug.Assert?
-
3Sure. And then the program will continue with whatever corruption of state was being detected in the Assert, and disaster will befall you in a way most heinous. Why don't you just stop doing whatever it is that is causing the Assertion to complain?Jeffrey L Whitledge– Jeffrey L Whitledge2011-05-23 19:50:49 +00:00Commented May 23, 2011 at 19:50
-
1@Jeffrey: though your point is well taken, presumably the assertion is erroneous; there should not be a way to make an assertion fire in the first place if the code is correct. The assertion probably indicates a bug in the library, not a bug in the caller's code. If Levi has determined that the bug is benign then it might be worthwhile to simply suppress the assertion until the code is fixed.Eric Lippert– Eric Lippert2011-05-23 19:54:35 +00:00Commented May 23, 2011 at 19:54
-
@Eric Lippert - I figured the library author failed to understand the correct useage of Assert (since it should never fire), but there must have been some reason for doing the check in the first place. Perhaps it is something for which an exception should be thrown. It seems unlikely that they would have added a test for a condition that doesn't matter. But then again maybe they did. If so, then perhaps another library without these errors-upon-errors should be sought! :)Jeffrey L Whitledge– Jeffrey L Whitledge2011-05-23 20:21:45 +00:00Commented May 23, 2011 at 20:21
4 Answers
If you can recompile the library, then Marc is correct; just recompile it without DEBUG defined and all the assertions will disappear.
If you can't do that, because say you don't have the source code of the library, then you can have your program clear out the Trace Listeners. That way when the assertion fires, it still fires but it does nothing. (When an assertion fires, it just checks to see what trace listeners are registered, and informs them about the assertion. No listeners means nothing happens.)
In that scenario you might consider replacing the default trace listener with a customer trace listener of your own that does something like logs the assertion to a file. That way you can review the log and see what assertions would have popped up in the "normal" execution of the debug version of the library.
Comments
Are you using a binary or do you have library source? In the former case, I suggest you contact your vendor to ask why they released a Debug build and also let them know about the Assert, which is a bug in their code (Assertion failures should never occur if code is executing as expected, even in exceptional cases.)
Comments
You can add the following section to your app.config file which inhibits breaking on Debug.Assert(false):
<configuration>
<system.diagnostics>
<assert assertuienabled="false"/>
</system.diagnostics>
</configuration>
For details see assert element and DefaultTraceListener.AssertUiEnabled Property.