[cfe-dev] Bug with -fvisibility-inlines-hidden flag

Weitian Leung via cfe-dev cfe-dev at lists.llvm.org
Sun Oct 18 23:28:19 PDT 2015


 I wonder if somebody could have a look at my mail and point me out what's
the problem. :-)

On 10/14/2015 05:07 PM, Weitian Leung wrote:

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).

-- 
*Regards*


-- 
*Regards*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151019/03a2b065/attachment.html>


More information about the cfe-dev mailing list