One way to avoid memory leak os to avoid using pointers.
std::set<shape>
And make appropriate changes to the code.
Otherwise, using shared pointers or other kind of smart pointers can really help avoiding leaks particulary if an exception occurs or if the code is not linear.