A fixture retains a non-owning pointer obtained from a local std::unique_ptr.
The unique pointer is then moved into a separately implemented owner's
constructor, which moves it into a member. The pointee outlives the fixture's
creation method, but cppcheck reports that the observer points to a local
variable.
No danglingLifetime diagnostic on Observer = local.get(). Observer refers to
the dynamically allocated callback, not the local smart-pointer object. Owner::CallbackInstance owns that allocation after both moves and remains alive
when the observer is used. The runtime assertion succeeds.
A fixture retains a non-owning pointer obtained from a local
std::unique_ptr.The unique pointer is then moved into a separately implemented owner's
constructor, which moves it into a member. The pointee outlives the fixture's
creation method, but cppcheck reports that the observer points to a local
variable.
owner.h:owner.cpp:observer.cpp:Version: Cppcheck 2.22.0 (Linux).
Save the three independent files together in a fresh directory, then run:
Expected
No
danglingLifetimediagnostic onObserver = local.get().Observerrefers tothe dynamically allocated callback, not the local smart-pointer object.
Owner::CallbackInstanceowns that allocation after both moves and remains alivewhen the observer is used. The runtime assertion succeeds.
Actual
The exact standalone invocation above emits:
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15154