【问题标题】:Boost unit tests fail with sigabrtsigabrt Boost 单元测试失败
【发布时间】:2015-07-22 22:07:37
【问题描述】:

我为它编写了一个动态库和单元测试。最近,我用 Valgrind 检查了库中是否存在任何内存泄漏,经过一番打磨后,我设法消除了每一个泄漏。但是在运行 boost 单元测试后,我遇到了一些来自 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)是无内存泄漏的,我假设它必须是 boost...

我可以做些什么来解决这个问题,以便我的单元测试能够运行而不会出现这些错误?或者有没有人告诉我如何以任何方式调试它?

【问题讨论】:

    标签: c++ unit-testing boost


    【解决方案1】:

    错误不在发布的代码中。

    Live On Coliru

    您将有自行调试。

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

    查看您是否将共享指针别名为其他共享指针。查看您是否在对象的构造函数中使用了shared_from_this(间接)。

    如果没有,只需消除原因。注释所有测试,直到错误消失。删除 TU。制作更多本地化的东西。等等等等,直到你有一个适合 SO 问题的样本并且仍然存在问题。

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


    另见:

    【讨论】:

    • 提前感谢您的帮助。我仍在试图找出它可能是什么......sdf::Element 的问题是,这是我使用的一个库。我无法直接访问其中的任何内容……至少我走在正确的道路上。我发现,如果我只是删除析构函数,所有测试用例都可以正常运行。这当然是不希望的,但是析构函数在使用我的共享库的普通 CLI 程序上没有问题...我现在将进一步调查这个问题,也许它是库中的问题...
    • 我接受了您的回答,因为它引导我朝着正确的方向解决问题。虽然这非常困难且耗时......非常感谢您的帮助:)
    猜你喜欢
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多