c++ - Boost 单元测试因 sigabrt 而失败

标签 c++ unit-testing boost

我为它写了一个动态库和单元测试。最近,我用 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...

我该怎么做才能解决这个问题,以便我的单元测试运行时不会出现这些类型的错误?或者有没有人告诉我如何以任何方式调试它?

最佳答案

错误不在发布的代码中。

Live On Coliru

您将自己调试它。

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

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

如果不是,就排除原因。注释所有测试,直到错误消失。删除 TU。制作更多 TU-local 的东西。等等,直到你有一个适合 SO 问题的样本并且仍然出现问题。

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


另见:

关于c++ - Boost 单元测试因 sigabrt 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31574865/

相关文章:

ios - Xcode 10 : Load the same . 项目目标和单元测试目标中的xml文件

javascript - stub 函数在被另一个函数调用时不会返回正确的值

.net - 如何将 boost::asio 套接字转换为 C++/CLI .Net 套接字?

C++ Ifstream 对象等于 nullptr 但它不是指针?

c++ - 函数中模板类的返回值不能去掉 "warning not all control paths return a value"

c++ - 填充 boost vector 或矩阵

c++ - 无法让 Ocean Optics OmniDriver 工作

php - 如何(真正)对适配器进行单元测试

c++ - 如何在 BGL 中按照(捆绑)属性提供的顺序迭代顶点和边?

c++ - 如何创建 std::string 包装器,将 ptr 保留到 std::string 并将 ptr 保留到创建该包装器实例的类中的特殊类函数?