<div dir="ltr">
  

    
  
  <div bgcolor="#FFFFFF" text="#000000">
    I wonder if somebody could have a look at my mail and point me out
    what's the problem. <span><span> :-) </span></span><br>
    <br>
    <div>On 10/14/2015 05:07 PM, Weitian Leung
      wrote:<br>
    </div>
    <blockquote type="cite">
      
      Hi all,<br>
      <br>
      Consider the following code (full source code can be found in
      attachment)<br>
      <br>
      object.h<br>
      <blockquote type="cite">#define
        _DESTRUCTOR_IN_HEADER<br>
        <br>
        class Object<br>
        {<br>
        public:<br>
            Object();<br>
        #ifdef _DESTRUCTOR_IN_HEADER<br>
            ~Object()<br>
            {<br>
            }<br>
        #else<br>
            ~Object();<br>
        #endif<br>
        <br>
        private:<br>
            class Counter<br>
            {<br>
            public:<br>
                Counter()<br>
                    : _count(0)<br>
                {<br>
                    printf("Counter: %p %d\n", this, _count);<br>
                }<br>
        <br>
                ~Counter()<br>
                {<br>
                    printf("~Counter: %p %d\n", this, _count);<br>
                }<br>
        <br>
                void operator++()    { ++_count; }<br>
                void operator--()    { --_count; }<br>
        <br>
            private:<br>
                int _count;<br>
            };<br>
        <br>
            class Foo<br>
            {<br>
            public:<br>
                Foo() { ++counter(); }<br>
                ~Foo() { --counter(); }<br>
        <br>
            private:<br>
                Counter& counter()<br>
                {<br>
                    static Counter s_counter;<br>
                    return s_counter;<br>
                }<br>
            } foo;<br>
        };<br>
      </blockquote>
      object.cpp
      <blockquote type="cite">#include
        "object.h"<br>
        <br>
        Object::Object()<br>
        {<br>
        }<br>
        <br>
        #ifndef _DESTRUCTOR_IN_HEADER<br>
        Object::~Object()<br>
        {<br>
        <br>
        }<br>
        #endif</blockquote>
      <br>
      Build the source to a shared library (compile with
      -fvisibility-inlines-hidden flag),<br>
      uses in main (another module)<br>
      <blockquote type="cite">    Object
        *obj = new Object;<br>
            delete obj;</blockquote>
      you may see the strange output when running<br>
      <blockquote type="cite">Counter:
        0x7f2ded933efc 0<br>
        Counter: 0x6012d4 0<br>
        ~Counter: 0x6012d4 -1<br>
        ~Counter: 0x7f2ded933efc 1</blockquote>
      <br>
      The Counter construct/destruct twice, the second one (Counter:
      0x6012d4 0) construct from<br>
       delete obj > Object::Foo::~Foo() > Object::Foo::counter()<br>
      <br>
      when comment out the line <b>#define _DESTRUCTOR_IN_HEADER </b>or

      remove the<b><br>
      </b><b>-fvisibility-inlines-hidden</b> flag, it works as expected<br>
      remove the compile flag<br>
      <blockquote type="cite">Counter:
        0x6013a4 0<br>
        ~Counter: 0x6013a4 0</blockquote>
      comment out #define _DESTRUCTOR_IN_HEADER<br>
      <blockquote type="cite">Counter:
        0x7f1eaa16629c 0<br>
        ~Counter: 0x7f1eaa16629c 0</blockquote>
      <br>
      A bit difference, as the address isn't the same (heap and stack)<br>
      <br>
      this code works with GCC (as least 5.2 as my test) with or without
      the -fvisibility-inlines-hidden<br>
      flag, of course it works with MSVC too.<br>
      <br>
      I don't known is this a bug with the flag or because the silly
      code makes it (as when constructor and<br>
      destructor defined in the same file, it works).<br>
      <br>
      
      
      
      
      
      
      
      
      <div>-- <br>
        <i>Regards</i></div>
    </blockquote>
    <br>
    <div>-- <br>
      <i>Regards</i></div>
  </div>

</div>