Opened 12 years ago

Closed 12 years ago

#5260 closed Bugs (fixed)

scope_guard safe_execute could support try/catch when BOOST_NO_EXCPTIONS not defined

Reported by: z_gerg@… Owned by: Joaquín M López Muñoz
Milestone: To Be Determined Component: multi_index
Version: Boost 1.46.0 Severity: Problem
Keywords: Cc:

Description

Hi,

Since I use boost library for many things, I thought I would take advantage of the scope guard implementation in boost/multi_index/detail/scope_guard.hpp. I know that is not standard practice, but thought it would be better then implementing my own ;-)

However I discovered the hard way (got an unhandled exception) that safe_execute does not support try/catch. From the comments

  • - safe_execute does not feature a try-catch protection, so we can
  • use this even if BOOST_NO_EXCEPTIONS is defined.

I verified that this is still the case in the latest release version. (I use an older version).

It seems it could support try/catch while still honoring BOOST_NO_EXCEPTIONS when defined by wrapping the try and catch portions in #ifdefs as follows

template<typename J> static void safe_execute(J& j) {

if(!j.dismissed_)

#ifndef BOOST_NO_EXCEPTIONS

try {

#endif

j.execute();

#ifndef BOOST_NO_EXCEPTIONS

} catch (...) { }

#endif }

By doing that when using exceptions it will work as the original code this is based on works. And when BOOST_NO_EXCEPTIONS is defined it will work as it does now.

The following example code will generate an unhandled exception with out the above mods. The same example with the above mods will also generate the unhandled exception if BOOST_NO_EXCEPTIONS is defined (you can uncomment the #define BOOST_NO_EXCEPTIONS to test that). It will not generate an unhandled exception when BOOST_NO_EXCEPTIONS is not defined.

#define BOOST_NO_EXCEPTIONS #include <iostream> #include <boost/multi_index/detail/scope_guard.hpp>

void bar() {

throw std::exception("testing");

}

void foo() {

boost::multi_index::detail::scope_guard barGuard = boost::multi_index::detail::make_guard(&bar);

}

int main(int argc, char* argv[]) {

foo(); return 0;

}

Change History (1)

comment:1 by Joaquín M López Muñoz, 12 years ago

Resolution: fixed
Status: newclosed

Really, the backup code provided to a scope guard should not destroy, but the proposed addition is simple enough and admittedly was in the original code by Alexandrescu. Change commited at [69540].

Note: See TracTickets for help on using tickets.