[Bug c++/101695] New: calling incorrect destructor of same-name class in anonymous namespaces in separate translation units
tom_maly at volny dot cz
gcc-bugzilla@gcc.gnu.org
Fri Jul 30 14:40:04 GMT 2021
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101695
Bug ID: 101695
Summary: calling incorrect destructor of same-name class in
anonymous namespaces in separate translation units
Product: gcc
Version: 11.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: tom_maly at volny dot cz
Target Milestone: ---
Created attachment 51225
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51225&action=edit
commit 079009b17cac42fb7c46adcee6055b4776b1360f
I have two cpp files, each with anonymous namespace with a class with same
name.
I store member function pointer to method that call the destructor.
Destructor from same translation unit is used in both cases.
Tested with g++ 9, 10, and 11 - all incorrect.
Also tested with clang 10 and msvc - both correct.
All source code to reproduce the bug (in addition to the attached archive) is
available at:
https://github.com/malytomas/gppBugAnonDestr
The github actions show results for all mentioned compilers.
More detailed info:
At least two translation units are required to separate the anonymous
namespaces.
Example command line used for compilation:
/usr/bin/g++-11 -Wall -Wextra -g -std=gnu++17 -MD -MT
CMakeFiles/gppBugAnonDestr.dir/sources/aaa.cpp.o -MF
CMakeFiles/gppBugAnonDestr.dir/sources/aaa.cpp.o.d -o
CMakeFiles/gppBugAnonDestr.dir/sources/aaa.cpp.o -c
/home/runner/work/gppBugAnonDestr/gppBugAnonDestr/sources/aaa.cpp
Example g++ version:
g++-11 (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
In the test code, a counter is used to detect which constructors/destructors
are called and an exception is thrown when number of constructors does not
match number of destructors.
Structure MemoryArena is used to create and destroy the Tester structures.
I could not reproduce the bug without it (using just free functions).
The struct Holder is very simplified std::unique_ptr.
Struct Delegate is simpler std::function. Simplifying it even further (making
it non template) also partially fixes the bug, therefore I keep it as is.
Feel free to contact me if you need any further information.
Thanks
More information about the Gcc-bugs
mailing list