Intel® oneAPI DPC++/C++ Compiler
Talk to fellow users of Intel® oneAPI DPC++/C++ Compiler and companion tools like Intel® oneAPI DPC++ Library, Intel® DPC++ Compatibility Tool, and Intel® Distribution for GDB*
790 Discussions

icpc - Thread_local an classes with "constructor = default" leads to multiple call to destructor

johanM
Beginner
442 Views

Hi,
we have in our code some thread_local homemade containers in order to don't reallocate them at each call of thread function, and recently we added "=default" in a constructor instead of "{}", and this result in a strange behavior  : at the end of the thread, the destructor began to be called as much as the function was called inside the thread, and as we have a free on a pointer in the destructor the application began to crash on "double free" error.

I have made a simple example of it:

 

 

#include <iostream>
#include <thread>

class youpela{
        float toto{};
        public:
        constexpr youpela() noexcept = default;
        ~youpela(){

                printf("delete_youpela\n");
                // Here if I free a pointer : double delete if my_func was called at least twice
        }

};


void my_func()
{
    thread_local youpela boom;

    printf("leaving my_func()\n");
}

void my_thread_func()
{
    my_func();
    my_func();
}

int main()
{

    printf("main\n");
    std::thread my_thread(my_thread_func);

    my_thread.join();

    return 0;
}

 


And on compiler explorer, this behavior is only observable by using icc (icpc in our case). GCC/icx don't have this behavior the constructor is only called once, instead of twice with icpc.

This behavior is clearly not expected, and in our case we call a lot of time this method, resulting in a huge loop on destruction for each threads I guess.

Can someone explain/fix this? It seems it is not fixed on latest icpc versions.

Thanks

0 Kudos
2 Replies
johanM
Beginner
441 Views

Little update, by removing "=default" and replacing by "{}" the destructor will only be called once.

0 Kudos
Viet_H_Intel
Moderator
374 Views

Intel C++ Compiler Classic (icc/icpc) has been end of life. We will not fix or release a new version of icc/icpc any more. Please migrate your code to oneAPI DPC++/C++ Compiler (icx/icpx)

0 Kudos
Reply