OT: Finding the Line of the Assert Fail?

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
User avatar
Steve Maughan
Posts: 1061
Joined: Wed Mar 08, 2006 7:28 pm
Location: Florida, USA
Contact:

OT: Finding the Line of the Assert Fail?

Post by Steve Maughan » Sat Mar 07, 2015 2:17 am

Hopefully a VS C expert can help me.

I'm tinkering with Maverick and have a bug. However, it only shows up on certain occasions after a UCI "stop" command (e.g. when in analysis mode and stepping through a game). It also only happens when Maverick is running in a GUI (e.g. Fritz). The dialog simply says "abort has been called"

Image

So my question is how can I figure out the line where there is an assert fail when the program is running in a GUI?

Thanks - Steve
http://www.chessprogramming.net - Maverick Chess Engine

jdart
Posts: 3815
Joined: Fri Mar 10, 2006 4:23 am
Location: http://www.arasanchess.org

Re: OT: Finding the Line of the Assert Fail?

Post by jdart » Sat Mar 07, 2015 4:28 am

You can start an instance of Visual C++ and attach it to the process that is giving the assert (Attach to Process is on the Debug menu).

--Jon

Sven
Posts: 3822
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

Re: OT: Finding the Line of the Assert Fail?

Post by Sven » Sat Mar 07, 2015 7:57 pm

Normally an assertion failure triggered through the assert() macro would cause an error message that contains source file and line number (__FILE__, __LINE__). But when running your program in a GUI you only get a MessageBox which unfortunately does not appear to show these details. I don't know a method to get this information displayed in that case.

Therefore attaching with a debugger seems to be the only way, as Jon stated.

To avoid this kind of annoying problem, but also to be able to set a breakpoint to the line where abort() is called (often I do not have the MSVC++ library source code at hand so I can't set a breakpoint into the code that implements the assert() macro!), I always use my own ASSERT() macro, and I define DEBUG in the debug version instead of defining NDEBUG in the release version. Example:

Code: Select all

// header file

#ifdef DEBUG
void raiseAssertionFailure(char const * file, int line, char const * expr);
#define ASSERT(expr) if (!(expr)) raiseAssertionFailure(__FILE__, __LINE__, #expr)
#else
#define ASSERT(expr)
#endif /* DEBUG */

// cpp file

#ifdef DEBUG
#include <cstdio>
#include <cstdlib>
#endif /* DEBUG */

#ifdef DEBUG
void raiseAssertionFailure&#40;char const * file, int line, char const * expr&#41;
&#123;
    fprintf&#40;stderr, "*** ASSERTION FAILED&#58; \"%s\" (%s, line %d&#41;\n", expr, file, line&#41;;
    abort&#40;);
&#125;
#endif /* DEBUG */
Of course using cerr << ... instead of fprintf() is fine here as well. Just don't make raiseAssertionFailure() a macro itself (which would be the same as doing fprintf + abort directly within ASSERT), it will bloat your code.

You can also use the _ASSERT or _ASSERTE macros but AFAIK these are Microsoft extensions so you lose portability.

User avatar
Steve Maughan
Posts: 1061
Joined: Wed Mar 08, 2006 7:28 pm
Location: Florida, USA
Contact:

Re: OT: Finding the Line of the Assert Fail?

Post by Steve Maughan » Sat Mar 07, 2015 8:16 pm

Jon & Sven,

AWESOME!!

I've never attached to a process before. But it did the trick. I managed to find the bug in 5 mins.

Many thanks,

Steve
http://www.chessprogramming.net - Maverick Chess Engine

User avatar
velmarin
Posts: 1600
Joined: Mon Feb 21, 2011 8:48 am

Re: OT: Finding the Line of the Assert Fail?

Post by velmarin » Sat Mar 07, 2015 8:16 pm

Here's a good explanation.
Almost always the debug versions are problems in the GUI. Try the relase versión.
https://msdn.microsoft.com/en-us/library/k089yyh0.aspx

Post Reply