[cfe-dev] strange behavior when compile with -fvisibility-inlines-hidden flag

Weitian Leung via cfe-dev cfe-dev at lists.llvm.org
Wed Dec 2 01:41:24 PST 2015


Hi all,

Consider the following code (full source code can be found in attachment)

object.h

#define _DESTRUCTOR_IN_HEADER

class Object
{
public:
    Object();
#ifdef _DESTRUCTOR_IN_HEADER
    ~Object()
    {
    }
#else
    ~Object();
#endif

private:
    class Counter
    {
    public:
        Counter()
            : _count(0)
        {
            printf("Counter: %p %d\n", this, _count);
        }

        ~Counter()
        {
            printf("~Counter: %p %d\n", this, _count);
        }

        void operator++()    { ++_count; }
        void operator--()    { --_count; }

    private:
        int _count;
    };

    class Foo
    {
    public:
        Foo() { ++counter(); }
        ~Foo() { --counter(); }

    private:
        Counter& counter()
        {
            static Counter s_counter;
            return s_counter;
        }
    } foo;
};

object.cpp

#include "object.h"

Object::Object()
{
}

#ifndef _DESTRUCTOR_IN_HEADER
Object::~Object()
{

}
#endif


Build the source to a shared library (compile with
-fvisibility-inlines-hidden flag),
uses in main (another module)

    Object *obj = new Object;
    delete obj;

you may see the strange output when running

Counter: 0x7f2ded933efc 0
Counter: 0x6012d4 0
~Counter: 0x6012d4 -1
~Counter: 0x7f2ded933efc 1


The Counter construct/destruct twice, the second one (Counter: 0x6012d4 0)
construct from
 delete obj > Object::Foo::~Foo() > Object::Foo::counter()

when comment out the line *#define _DESTRUCTOR_IN_HEADER *or remove the
*-fvisibility-inlines-hidden* flag, it works as expected
remove the compile flag

Counter: 0x6013a4 0
~Counter: 0x6013a4 0

comment out #define _DESTRUCTOR_IN_HEADER

Counter: 0x7f1eaa16629c 0
~Counter: 0x7f1eaa16629c 0


A bit difference, as the address isn't the same (heap and stack)

this code works with GCC (as least 5.2 as my test) with or without the
-fvisibility-inlines-hidden
flag, of course it works with MSVC too.

I don't known is this a bug with the flag or because the silly code makes
it (as when constructor and
destructor defined in the same file, it works).

It seems that my last post failed, seems no body answers me, so I repost
again, sorry.
If this is the wrong place, please let me know where to post, thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151202/46b50739/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: demo.tar.xz
Type: application/x-xz
Size: 832 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151202/46b50739/attachment.bin>


More information about the cfe-dev mailing list