https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61674
--- Comment #6 from Alex Finch <A.Finch at lancaster dot ac.uk> --- I guess I should have made that clear. The class has a static variable: static int fLockCount; which counts how many times an object of the class is created. If it is >1, then IsLocked() returns true. The destructor has a line --fLockCount; which is not executed if the destructor is not called. This breaks the program. The destructor should not be optimised out precisely because it modifies the static variable.