c++ - Boost 测试设置错误 : memory access violation

标签 c++ boost boost-test

在编译和运行以下文件后运行可执行文件时出现上述错误。

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/bind.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/filesystem/fstream.hpp>
#include "index/DatabaseGroup.hpp"

using namespace boost::unit_test;

namespace indexing {

class ForwardDBTest {

    /// A pointer to the database group object.
    DatabaseGroup& databaseGroup;

    std::string databaseName;
public:

    ~ForwardDBTest() {
    }
    ;

    ForwardDBTest(DatabaseGroup& databaseGroup_, std::string dbName) :
            databaseGroup(databaseGroup_), databaseName(dbName) {
    }

    void boostTestCreateDB() {
        databaseGroup.createDatabase(databaseName, databaseName);
    }

};

class testSuites: public test_suite {
public:
    testSuites() :
            test_suite("test_suite") {
        std::string db_location = "home/girijag/ripe/ripe_db";
        std::cout << "hello" << std::endl;
        int concurrency = 0;
        std::string db_cache_policy = "AllMem";
        boost::shared_ptr<DatabaseGroup> db = boost::shared_ptr<DatabaseGroup>(
                new DatabaseGroup(db_location, concurrency, db_cache_policy));
        std::string dbName = "DB1";
        boost::shared_ptr<ForwardDBTest> instance(
                new ForwardDBTest(*db, dbName));
        test_case* boostTestCreateDB_test_case = BOOST_CLASS_TEST_CASE(
                &ForwardDBTest::boostTestCreateDB, instance);
        add(boostTestCreateDB_test_case);
    }

    ~testSuites() {
    }
    ;

};

test_suite* init_unit_test_suite(int argc, char** argv) {

    test_suite* suite(BOOST_TEST_SUITE("Master Suite"));
    suite->add(new testSuites());
    return suite;
}

}'

请让我知道我应该如何解决这个问题? 我收到如下错误:-

Test setup error: memory access violation at address: 0x00000021: no mapping at fault address

过去两天我一直在努力弄清楚我的问题是什么

最佳答案

代码中有很多令人不安的东西,发布问题时似乎必须丢失一些格式,否则就没有编译的机会。 (例如,}’ ?!)

对于初学者,您不应该将 init_unit_test_suite(int, char**) 放在 indexing 命名空间中,随后定义 BOOST_TEST_MAIN 也没有意义 - 您最终会得到上述 init_unit_test_suite(int, char**) 方法的多个定义。

在您的情况下,套件应该简单地在主测试套件中注册,无需从方法返回指向它的指针。

这是一个最小的示例,您可以根据自己的目的使用扩展。它遵循您的结构,但省略了不相关的细节:

#include <boost/test/included/unit_test.hpp>
#include <iostream>

using namespace boost::unit_test;

namespace indexing {

class ForwardDBTest { 
public:
    void boostTestCreateDB() { std::cout << __FUNCTION__ << std::endl; }
};

class TestSuite : public test_suite {
public:
    TestSuite() : test_suite("test_suite") {
        boost::shared_ptr<ForwardDBTest> instance(new ForwardDBTest);
        add(BOOST_CLASS_TEST_CASE(&ForwardDBTest::boostTestCreateDB, instance));
    }
};

} // namespace indexing

test_suite* init_unit_test_suite(int, char**) {
    framework::master_test_suite().add(new indexing::TestSuite);
    return 0;
}
/* Output:
Running 1 test case...
boostTestCreateDB

*** No errors detected
*/

关于c++ - Boost 测试设置错误 : memory access violation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21934179/

相关文章:

c++ - boost::shared_ptr 引用计数确实解决了 tr1::shared_ptr 的原始指针

c++ - 如何分析 g++/std::mutex 下的锁争用?

c++ - 在 windows 中构建 boost 的子集

c++ - Boost Test BOOST_CHECK_EQUAL 类型可转换为数组

c++ - 我可以将 BOOST_TEST_CASE 用于非静态类成员函数吗?

c++ - 如何在 C++ (sfml) 中获取图像中像素的颜色

C++ 模板 : Select different type based on value of template parameter

c++ - 具有常量表达式 : error C2975 with VC++2008 的模板

c++ - Boost.Log 同时记录到文件和标准输出?

c++ - BOOST_CHECK_EQUAL(和衍生品)添加自定义消息