简体   繁体   English

x86和ARM上的共享指针

[英]Shared pointer on x86 and ARM

In a shared library (.so) I define a std::shared_ptr to a class object which is returned to the caller accross the library boundary to the main routine which is a Qt5.4 project. 在共享库(.so)中,我为类对象定义了一个std :: shared_ptr,该对象跨越库边界与主例程(即Qt5.4项目)一起返回给调用者。 There the pointer is used in a if statement. 该指针在if语句中使用。 As the bool operation is the last owner of the shared pointer it is deleted after finishing this operation and the destructor is called. 由于布尔操作是共享指针的最后所有者,因此在完成此操作后将其删除并调用析构函数。

.so file (an Autotools-project): .so文件(一个Autotools项目):

#define STD_SHARED_PTR std::shared_ptr
#define STD_WEAK_PTR std::weak_ptr

typedef STD_SHARED_PTR<RenderingControl> RDCH;
typedef STD_WEAK_PTR<RenderingControl> WEAK;

class MediaRenderer {
    public:
        RDCH rdc();
}

class RenderingControl {
    public:
        RenderingControl();
        virtual ~RenderingControl();
}

RenderingControl::RenderingControl() {
    ...
}

RendeneringControl::~RenderingControl() {
    cerr << "Destructor called" << endl;
}

RDCH MediaRenderer::rdc() {

    RDCH rdcl = RDCH(new RenderingControl());
    long foo = rdcl.use_count();

    WEAK rdc = rdcl; 
    return rdcl;
}

.cpp (a Qt5.4 project): .cpp(一个Qt5.4项目):

typedef STD_SHARED_PTR<RenderingControl> MRDH;

MRDH renderer = MRDH(new MediaRenderer());

if (renderer->rdc()) {
    ...
    return;
}

Everything works fine on a x86 machine compiled with either Qt4.8 or Qt5.4. 在使用Qt4.8或Qt5.4编译的x86机器上,一切正常。 The destructor is called after finishing the if statement. 完成if语句后将调用析构函数。 Cross compiled for an ARM (Raspberry Pi 2) using Qt5.4, however, the destructor is not called. 使用Qt5.4为ARM(Raspberry Pi 2)交叉编译,但是不调用析构函数。 If I additionally add use_count() for debugging, it yields 1 in both the .so and the .cpp file on the x86, but 1 in the .so and 0 in the .cpp for the ARM. 如果我另外添加use_count()进行调试,则在x86的.so和.cpp文件中都将产生1,而对于ARM的.so和.cpp中将产生1。

If I compile on ARM using Qt4.8 everything is fine on ARM, too. 如果我使用Qt4.8在ARM上进行编译,那么在ARM上也一切正常。 But why does it not work on ARM using Qt5.4? 但是,为什么它不能在使用Qt5.4的ARM上运行?

Thank you! 谢谢!

Obviously, the reason was different versions of libstdc++ I have on the same system for compiling a autotools project and a Qt project. 显然,原因是我在同一系统上使用不同版本的libstdc ++来编译autotools项目和Qt项目。 However, even if I solved the problem by converting the library into an Qt project ensuring the use of the same libstdc++ for both parts, I do not understand why there are different versions on the same machine? 但是,即使我通过将库转换为Qt项目(确保在两个部件上使用相同的libstdc ++)解决了问题,我也不明白为什么同一台计算机上存在不同的版本? Is this a specific feature of Qt? 这是Qt的特定功能吗? Mayby, anybody could explain... Mayby,任何人都可以解释...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM