0

I'm using SAL to make sure that all code paths that create an object X should call X::work() before destroying it.

#include <sal.h>

class X {
    bool worked = false;
public:
    _Post_satisfies_(!worked)
    X() : worked(false) {}

    _Post_satisfies_(worked)
    void work() {
        worked = true;
    }

    _Pre_satisfies_(worked)
    ~X() {
    }
};

int main() {
    X x;
    X y; // Does not call work() but still passes the test anyway
    x.work();
}

When I remove x.work(), then there goes an error as intended:

warning C28020: The expression 'this->worked' is not true at this call.

But soon as I add work() for one object x, the other one y also seems to pass the test. Is there some problem in my annotation?

nedsociety
  • 88
  • 8
  • What optimization level are you using? Is the compiler optimizing-away `y` entirely? – Dai Jun 08 '18 at 06:29
  • I'm using default Debug configuration for VS2015 (except "Analysis on Build" turned on) on empty Win32 Console Application template, so I doubt that it was optimized out. – nedsociety Jun 08 '18 at 08:53

0 Answers0