boost::variant 出现问题,链接器出现段错误

发布于 2024-08-22 05:29:24 字数 1735 浏览 5 评论 0原文

我有一个有 7 种类型的 boost 变体。当我尝试使用最后两种类型时,链接器出现段错误。我在 64 位 Linux 机器上使用 g++(SuSE Linux 上的 gcc 版本 3.3.3),得到的错误是

collect2: ld terminated with signal 11 [Segmentation fault]

无论我将类型放入什么顺序,最后两个都会在我尝试使用时导致段错误他们。有什么想法为什么会发生这种情况吗?

代码:

typedef boost::tuple<std::string, Class1::Ptr> Class1Tuple;
typedef boost::tuple<std::string, Class2::Ptr> Class2Tuple;
typedef boost::tuple<std::string, Class3::Ptr> Class3Tuple;
typedef boost::tuple<std::string, Class4::Ptr> Class4Tuple;
typedef boost::tuple<std::string, Class5::Ptr> Class5Tuple;
typedef boost::tuple<std::string, Class6::Ptr> Class6Tuple;
typedef boost::tuple<std::string, Class7::Ptr> Class7Tuple;

typedef boost::variant< Class1Tuple, Class2Tuple, Class3Tuple,
                        Class4Tuple, Class5Tuple, Class6Tuple,
                        Class7Tuple > ClassTupleItem;

ClassX::Ptr 是指向该类的 boost 共享指针。 Ptr 被定义为类本身内部的 typedef ,

struct Class1
{
 typedef boost::shared_ptr<Class1> Ptr;
    ...
    ...
}

当我尝试使用 boost 变体中的最后两种类型时,

Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // works fine since I used Class1Tuple

Class6Tuple tup2("str", pointer2);
ClassTupleItem(tup2); // causes a segfault.

就像我将 boost::variant 定义为(交换 Class6 和 Class1)一样,

typedef boost::variant< Class6Tuple, Class2Tuple, Class3Tuple,
                        Class4Tuple, Class5Tuple, Class1Tuple,
                        Class7Tuple > ClassTupleItem;

然后在编译时出现段错误代码

Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // worked earlier

I have a boost variant with 7 types in it. When I try to use the last two types, the linker segfaults. I am using g++ (gcc version 3.3.3 on SuSE Linux) on a 64 bit linux machine and the error that I get is

collect2: ld terminated with signal 11 [Segmentation fault]

It doesnt matter what order I put the types in, the last two will cause a segfault when I try to use them. Any ideas why this would be happening?

Code:

typedef boost::tuple<std::string, Class1::Ptr> Class1Tuple;
typedef boost::tuple<std::string, Class2::Ptr> Class2Tuple;
typedef boost::tuple<std::string, Class3::Ptr> Class3Tuple;
typedef boost::tuple<std::string, Class4::Ptr> Class4Tuple;
typedef boost::tuple<std::string, Class5::Ptr> Class5Tuple;
typedef boost::tuple<std::string, Class6::Ptr> Class6Tuple;
typedef boost::tuple<std::string, Class7::Ptr> Class7Tuple;

typedef boost::variant< Class1Tuple, Class2Tuple, Class3Tuple,
                        Class4Tuple, Class5Tuple, Class6Tuple,
                        Class7Tuple > ClassTupleItem;

ClassX::Ptr is a boost shared pointer to that class. Ptr is defined as a typedef inside the class itself as below

struct Class1
{
 typedef boost::shared_ptr<Class1> Ptr;
    ...
    ...
}

when I try to use the last two types in the boost variant as in

Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // works fine since I used Class1Tuple

Class6Tuple tup2("str", pointer2);
ClassTupleItem(tup2); // causes a segfault.

if I define the boost::variant as (interchange Class6 and Class1)

typedef boost::variant< Class6Tuple, Class2Tuple, Class3Tuple,
                        Class4Tuple, Class5Tuple, Class1Tuple,
                        Class7Tuple > ClassTupleItem;

then I get a segfault when compiling this code

Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // worked earlier

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2024-08-29 05:29:25

它看起来像是一个编译器/链接器错误:任何 C++ 代码都不应该在编译器/链接器中导致段错误。

顺便问一下,如何编译这段代码? 指针是如何声明的?

Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // works fine since I used Class1Tuple

Class6Tuple tup2("str", pointer);
ClassTupleItem(tup2); // causes a segfault.

如果类是这样声明的,对于 Class1Tuplepointer 应该是 shared_ptr,对于 Class6Tuple > 它应该是不同的类型,shared_ptr

struct Class1
{
    typedef boost::shared_ptr<Class1> Ptr;
    /* ... */
};

/* ... */

struct Class6
{
    typedef boost::shared_ptr<Class6> Ptr;
    /* ... */
};

编辑:以下代码可以使用 g++ 3.3.6 正确编译。目前我无法在 gcc 3.3.3 和 SUSE Linux 上测试它。请尝试编译它并查看链接器是否仍然出现段错误。

#include <boost/shared_ptr.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/variant.hpp>
#include <string>

struct Class1
{
    typedef boost::shared_ptr<Class1> Ptr;
    /* .... */
};

struct Class2
{
    typedef boost::shared_ptr<Class2> Ptr;
    /* .... */
};

struct Class3
{
    typedef boost::shared_ptr<Class3> Ptr;
    /* .... */
};

struct Class4
{
    typedef boost::shared_ptr<Class4> Ptr;
    /* .... */
};

struct Class5
{
    typedef boost::shared_ptr<Class5> Ptr;
    /* .... */
};

struct Class6
{
    typedef boost::shared_ptr<Class6> Ptr;
    /* .... */
};

struct Class7
{
    typedef boost::shared_ptr<Class7> Ptr;
    /* .... */
};

typedef boost::tuple<std::string, Class1::Ptr> Class1Tuple;
typedef boost::tuple<std::string, Class2::Ptr> Class2Tuple;
typedef boost::tuple<std::string, Class3::Ptr> Class3Tuple;
typedef boost::tuple<std::string, Class4::Ptr> Class4Tuple;
typedef boost::tuple<std::string, Class5::Ptr> Class5Tuple;
typedef boost::tuple<std::string, Class6::Ptr> Class6Tuple;
typedef boost::tuple<std::string, Class7::Ptr> Class7Tuple;

typedef boost::variant< Class1Tuple, Class2Tuple, Class3Tuple,
                        Class4Tuple, Class5Tuple, Class6Tuple,
                        Class7Tuple > ClassTupleItem;

int main()
{
    Class1::Ptr pointer;
    Class1Tuple tup("str", pointer);
    (ClassTupleItem(tup)); // Temporary object
    ClassTupleItem item(tup);

    Class6::Ptr pointer2;
    Class6Tuple tup2("str", pointer2);
    (ClassTupleItem(tup2)); // Temporary object
    ClassTupleItem item2(tup2);
}

It looks like a compiler/linker bug: no C++ code should ever cause segfaults in the compiler/linker.

By the way, how do you get this code to compile? How is pointer declared?

Class1Tuple tup("str", pointer);
ClassTupleItem(tup); // works fine since I used Class1Tuple

Class6Tuple tup2("str", pointer);
ClassTupleItem(tup2); // causes a segfault.

If the classes are declared like this, for Class1Tuple, pointer should be a shared_ptr<Class1>, and for Class6Tuple it should be of a different type, shared_ptr<Class6>.

struct Class1
{
    typedef boost::shared_ptr<Class1> Ptr;
    /* ... */
};

/* ... */

struct Class6
{
    typedef boost::shared_ptr<Class6> Ptr;
    /* ... */
};

Edit: The following code compiles correctly with g++ 3.3.6. I am not able to test it on gcc 3.3.3 and SUSE Linux at the moment. Please try to compile this and see if the linker still gives a segfault.

#include <boost/shared_ptr.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/variant.hpp>
#include <string>

struct Class1
{
    typedef boost::shared_ptr<Class1> Ptr;
    /* .... */
};

struct Class2
{
    typedef boost::shared_ptr<Class2> Ptr;
    /* .... */
};

struct Class3
{
    typedef boost::shared_ptr<Class3> Ptr;
    /* .... */
};

struct Class4
{
    typedef boost::shared_ptr<Class4> Ptr;
    /* .... */
};

struct Class5
{
    typedef boost::shared_ptr<Class5> Ptr;
    /* .... */
};

struct Class6
{
    typedef boost::shared_ptr<Class6> Ptr;
    /* .... */
};

struct Class7
{
    typedef boost::shared_ptr<Class7> Ptr;
    /* .... */
};

typedef boost::tuple<std::string, Class1::Ptr> Class1Tuple;
typedef boost::tuple<std::string, Class2::Ptr> Class2Tuple;
typedef boost::tuple<std::string, Class3::Ptr> Class3Tuple;
typedef boost::tuple<std::string, Class4::Ptr> Class4Tuple;
typedef boost::tuple<std::string, Class5::Ptr> Class5Tuple;
typedef boost::tuple<std::string, Class6::Ptr> Class6Tuple;
typedef boost::tuple<std::string, Class7::Ptr> Class7Tuple;

typedef boost::variant< Class1Tuple, Class2Tuple, Class3Tuple,
                        Class4Tuple, Class5Tuple, Class6Tuple,
                        Class7Tuple > ClassTupleItem;

int main()
{
    Class1::Ptr pointer;
    Class1Tuple tup("str", pointer);
    (ClassTupleItem(tup)); // Temporary object
    ClassTupleItem item(tup);

    Class6::Ptr pointer2;
    Class6Tuple tup2("str", pointer2);
    (ClassTupleItem(tup2)); // Temporary object
    ClassTupleItem item2(tup2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文