使用sigabrt进行Boost单元测试失败

时间:2015-07-22 22:07:37

标签: c++ unit-testing boost

我为它写了一个动态库和单元测试。最近,我用Valgrind检查了库的任何内存泄漏,经过一些抛光后我设法摆脱了每一次泄漏。但是在运行了升压单元测试之后,我遇到了几个来自boost的奇怪错误,我找不到原点......

以下是完整的控制台错误日志:

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.

在单元测试的结果中,它还针对上述每个错误说明如下:

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

奇怪的是,没有任何异常的每一个BOOST_CHECK_*都没有问题,但最终一些(并非所有!)测试函数都会因上述错误而失败...

这是我的单元测试类的一部分,我在上面的第72行标记了注释。由于错误始终相同,因此我将测试类切割为包含一个示例。

#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()

之后,我用Valgrind检查了已编译的单元测试程序并发现了几个内存泄漏...我不知道这是否相关,但由于我的库(根据Valgrind)内存泄漏,我假设它必须得到提升......

我该怎么做才能解决问题,以便我的单元测试能够在没有这些错误的情况下运行?或者有人为我提供了如何以任何方式调试它的提示吗?

1 个答案:

答案 0 :(得分:1)

错误不在发布的代码中。

<强> Live On Coliru

自行调试。

问题似乎是释放未初始化的sdf::Element共享指针。查看您是否使用可能已链接到可执行文件和动态库的任何静态数据(可能它们已在头文件中定义)。

查看是否有共享指针别名其他共享指针。看看你是否从对象的构造函数中间接使用了shared_from_this

如果没有,只是消除原因。评论所有测试,直到错误消失。删除TU。制作更多东西TU-local。等等,直到你有一个适合SO问题的样本,但仍然存在问题。

可能之前,您会在此之前发现问题的根源。如果没有,我们仍然会提供帮助(甚至更多)。

另见: