Weird behaviour for ROOT 6.11/01 with unique_ptr

I have ROOT 6.04/02 installed in my office and 6.11/01 at home. I tried to run something I’ve written in my office at home and "illegal instruction " error appears. It is weird because my code run perfectly fine in my office. After debugging my code, I found out the problem arises because I failed to declare the destructor of an abstract class virtual. The following code illustrates my problem:

#include <iostream>

class test
        /*virtual*/ ~test(){}; // without virtual declaration, it causes problem explained below
        virtual void cout() = 0;

class test1 : public test
        void cout() { std::cout << " this is test1\n";};

class test2 : public test
        void cout() { std::cout << " this is test2\n";};


Save the code to Classtest.h, load it with “.L Classtest.h” and run the following script "for(int i = 1; i < 2; ++i){ std::unique_ptr<test> a(new test1);}". It runs without problems on ROOT 6.04/02 and ROOT 6.10/02, but on ROOT 6.11/01, it gives "illegal instruction ". By uncommenting virtual keyword, the problems is solved. Why is that so? Are there changes in how the interpreter interpret macro?

The reason is that the version without “virtual” causes UB.

In the C++ standard, you can find that in section [expr.delete], §3:

In the first alternative (delete object), if the static type of the object to be deleted is different from its
dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the
static type shall have a virtual destructor or the behavior is undefined.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.