Smart pointers don't fully save you from dangling references and PVS-Studio have a proof: V758 The 'graph' reference becomes invalid when smart pointer returned by a function is destroyed. 'create' returns a smart pointer to the 'FlannNeighborhoodGraphImpl' type, and its object reference count is one. 'graph' is created for the value of this smart pointer while the object reference count remains unchanged. But the pointer is a temporary and goes out of scope immediately after that line. Ref points to a destroyed object. 'for' references invalid. Result: undefined behavior. How to fix? Keep the smart pointer so that the 'FlannNeighborhoodGraph' object stays until the end of the block and limit the 'graph' visibility area. We've already reported the bug to the developers and created an issue: https://lnkd.in/dpm2vJ5H (And some of bugs already fixed, you can check the last OpenCV release notes) If you want to see what else our analyzer found in the OpenCV project, read the article: https://lnkd.in/drWfZCaC #CplusPlus #Cpp #OpenCV #Programming #Debugging