简体   繁体   中英

Boost unit tests fail with sigabrt

I wrote a dynamic library and unit tests for it. Recently, I checked the library with Valgrind for any memory leaks and after some polishing I managed to get rid of every single leak. But after running the boost unit tests, I encountered several weird errors from boost and I just can't find the origin...

Here is the complete console error log:

nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.
nddlgen-core-test: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = sdf::Element; typename boost::detail::sp_member_access<T>::type = sdf::Element*]: Assertion `px != 0' failed.

In the results of the unit tests, it also says for each of those errors above something like the following:

../src/base/Controller_Test.cpp(72): Exception: signal: SIGABRT (application abort requested)
Last check point was here.

The weird thing is, that every single BOOST_CHECK_* without any exception passes without a problem, but eventually some (not all!) test functions would fail anyway with the errors mentioned above...

Here is a part of my unit test class, I marked line 72 above with a comment. Since the error is always the same, I cut the test class to contain one example.

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ControllerTest

#include <boost/test/included/unit_test.hpp>
#include <boost/algorithm/string.hpp>

#include <nddlgen/Controller.h>

using namespace std;
using namespace nddlgen;
using namespace boost;
using namespace boost::unit_test;

struct UnitUnderTest
{
    UnitUnderTest()
    {
        c = new Controller(&this->errorText);
    }

    ~UnitUnderTest()
    {
        boost::checked_delete(c);
    }

    Controller* c;
    string errorText;
};

string existingFile = "res/testmodel.sdf";
string corruptedFile = "res/corruptedmodel.sdf";

/**
 * ======================================================
 * Tests for normal operation
 * ======================================================
 */
BOOST_AUTO_TEST_SUITE (ControllerTestNormalBehaviour)

    /**
     * Test if the output file names match the expected behavior.
     */
    BOOST_AUTO_TEST_CASE (testGetOutputFileNames)
    {
        UnitUnderTest uut;
        string actualModelOutputFileName;
        string actualInitialStateOutputFileName;

        // Should work since a file identifier has not been set yet
        BOOST_CHECK_EQUAL(uut.c->setFileIdentifier(existingFile), true);

        actualModelOutputFileName = uut.c->getModelsOutputFileName();
        actualInitialStateOutputFileName = uut.c->getInitialStateOutputFileName();

        BOOST_CHECK_EQUAL(actualModelOutputFileName, "testmodel-model.nddl");
        BOOST_CHECK_EQUAL(actualInitialStateOutputFileName, "testmodel-initial-state.nddl"); // This is line 72
    }

BOOST_AUTO_TEST_SUITE_END()

After that, I checked the compiled unit test program with Valgrind and found several memory leaks... I don't know if this is relevant, but since my library is (according to Valgrind) mem-leak-free, I assume that it must be boost...

What can I do to fix the problem so that my unit tests will run without these kind of errors? Or does anyone got a tip for me for how I could debug this in any way?

The error is not in the code posted.

Live On Coliru

You'll have to debug this yourself.

The problem appears to be with freed or uninitialized shared pointers to sdf::Element . See whether you use any static data that could have become linked into both the executable and the dynamic library (perhaps they're defined in header files).

See whether you have shared pointers aliasing other shared pointers. See if you used shared_from_this (indirectly) from the object's constructor.

If not, just eliminate causes. Comment all tests until the error disappears. Remove TUs. Make more stuff TU-local. Etc. etc. right until you have a sample that fits into an SO question and still exhibits the problem.

Likely, you'll spot the source of the problem before that. If not, we're still here to help (even more).


See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM