Author Topic: Boost assertions closes the debugger  (Read 14129 times)

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Boost assertions closes the debugger
« on: November 01, 2011, 05:08:24 pm »
Hi together!

I've searched for this topic in the forum but I have found nothing related.

I'm using Boost libraries to test my projects in CB, but I've found that when I use BOOST_ASSERT macro, the application closes and the developer has no information about what happened.
Why doesn't CB stop at the line the assertion fails?
Is it the normal behaviour?
Should I configure something?
Must I execute assertions in another way?

Thanks in advance to all of you.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11638
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #1 on: November 01, 2011, 05:15:37 pm »
Have you tried the debugger branch?
What is your OS/C::B version/GCC/boost/GDB?

Simple code to reproduce the problem?
Does normal assert(false) works?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #2 on: November 01, 2011, 05:17:34 pm »
> Why doesn't CB stop at the line the assertion fails?

Why should it? The assertion mechanism knows nothing about the debugger, and the debugger knows nothing about the assertion mechanism.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11638
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #3 on: November 01, 2011, 05:24:33 pm »
Because the assertion mechanism most of the times causes the application to crash, at least the assert(false) macro, does so.
If the app crashes the debugger should catch it and the user should be able to inspect the reason for the crash.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #4 on: November 01, 2011, 05:26:23 pm »
If you want to write your own code to process the the BOOST_ASSERT, this is how to do it:

#include <iostream>
#define BOOST_ENABLE_ASSERT_HANDLER
#include <boost/assert.hpp>
using namespace std;

namespace boost
{
    void assertion_failed(char const * expr, char const * function, char const * file, long line) {
      cout << "BOOST assertion failed\n";
   }
}

int main()
{
   BOOST_ASSERT( 1 == 0 );
   cout << "Hello world!" << endl;
   return 0;
}


Replace:

     cout << "BOOST assertion failed\n";

with whatever you need to do to enter the debugger, though your stack frame will probably be off.

See also http://www.boost.org/doc/libs/1_36_0/libs/utility/assert.html. But at the end of the day, assertions are not great debugging tools.

« Last Edit: November 01, 2011, 05:28:38 pm by Neil Butterworth »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11638
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #5 on: November 01, 2011, 05:43:11 pm »
Neil, please lets wait for the ThundThund to provide details about his problem.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #6 on: November 01, 2011, 05:50:45 pm »
Why? Surely the code I posted is useful and of interest to anyone using BOOST_ASSERT?

Further information, it appears that BOOST_ASSERT calls std::abort() on an assertion failure. Although I can see a debugger catching the abort, I can't see how it can catch the line at which the abort occurred, which is what the OP was asking about.


Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #7 on: November 01, 2011, 06:47:43 pm »
Thanks for all your answers.

Windows7/CB 10.05/MinGW (GCC 4.4.1)/ Boost 1.46.1

Well, the problem is that I'm coming from Visual Studio, and there the assertions break the execution at the line they fail, giving you the chance to review variable values and so, like a breakpoint. Even more, you can press F5 again and continue debugging (the program continues normally).
I expected CB has the same feature or a way to emulate it.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11638
    • Travis build status
Re: Boost assertions closes the debugger
« Reply #8 on: November 01, 2011, 07:06:09 pm »
ThundThund:
1. can you provide a minimal code sample.
2. try the debuggers branch nightly build

Neil: Stop the off topic, please, ThundThund has a concrete problem, so lets concentrate on it. And yes, C::B can put the current position marker on the line of the ASSERT, if the option 'When stopping, auto-switch to the first valid frame...' is enabled.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #9 on: November 01, 2011, 07:09:54 pm »
> Stop the off topic, please

I certainly may be wrong, but I am definitely not off-topic.

> ThundThund has a concrete problem

Which I am addressing. My approach may be different from yours, but that doesn't make it "wrong" (though it might be)  or "off-topic".
« Last Edit: November 01, 2011, 07:11:30 pm by Neil Butterworth »

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #10 on: November 01, 2011, 07:15:01 pm »
Well, the scenario is simple: I have one library, with many classes. In the method of one of the classes, I put BOOST_ASSERT(var). I link that library to my application, instance the class and then call the method. The debugger stops and I can't know what happened.


Note: Another thing I have to say is that breakpoints don't pause the debugger when I put them anywhere outside my "main.cpp file", don't know if it's related, maybe I'll post this in another thread.

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #11 on: November 01, 2011, 07:39:42 pm »
For me this code

#include <iostream>
#include <boost/assert.hpp>
using namespace std;


void f() {
  BOOST_ASSERT( 1 == 0 );
  // **** here ***
}

void g() {
   f();
}

int main()
{
   g();
   cout << "Hello world!" << endl;
   return 0;
}


When compiled with -g, and run in the debugger causes a break in the debugger. If I then go to the "Call Stack" debugger window and click on the the last entry there, I get the cursor moved to the line just after the BOOST_ASSERT. This is nowhere near as pretty as the MS debugger, I know, but we are talking gdb here!

This is with Win7, latest TDM, and a fairly recent CB nightly - SVN 7385.


Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #12 on: November 01, 2011, 07:56:15 pm »
I've copied and pasted that code, and the debugger doesn't break for me, it stops at all. :(
I don't know what I'm doing wrong. I have the option "When stopping..." and the -g flag activated.

zabzonk

  • Guest
Re: Boost assertions closes the debugger
« Reply #13 on: November 01, 2011, 08:01:13 pm »
Are you running in it in the debugger - i.e. from the Debug|Start menu option? And have you got the "Call Stack" window open? And if you don't get a break, what does  happen?

Offline ThundThund

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Boost assertions closes the debugger
« Reply #14 on: November 01, 2011, 08:05:26 pm »
Yes, Debug->Start. I've the Call stack window open. Imagine that you press the Stop debugging button at the moment the program starts, that happens, tha debugging finishes at the moment the assert is executed.