If you run this example and set a breakpoint in the destructor of 'A' (main.cpp:20) to see when the object is deleted, you will see that 'A' objects are deleted in line 53, that is :
std::unique_ptr<A> element = dequeue();
How is this possible?? The next instruction is 'element->print()', which is still using an object that is already deleted.
Well, the behaviour of the program is correct, it's just that the debug information that GCC generates is misleading. It has nothing to do with unique_ptr or anything in your program. It always does it this way.
Below is a simplified version of your program that exhibits the exact same phenomenon. The strings are outputted in the correct order but if you place a breakpoint in the destructor the stack trace will show the destructor as being called on line 20.
#include <stdio.h>
class A
{
public:
void print()
{
printf("Just a dummy method\n");
}
~A()
{
printf("A destructor\n");
}
};
int main()
{
A element;
element.print();
}
Just a dummy method
A destructor
If you use Compiler Explorer to look at the code (https://godbolt.org/z/3fi_sw) you can clearly see which instructions are being mapped to which lines by comparing the background colours (A element; and call A::~A() are both coloured yellow). If you switch compiler to Clang (https://godbolt.org/z/-_TYas) you'll see that the destructor is instead mapped to the closing } where the object goes out of scope, which is probably more intuitive.