Variable read after move
A variable in a moved-from state is used in an operation that has preconditions
Since R2025a
Description
Polyspace® reports this defect when both of these conditions are true:
The content of an object is moved by using
std::move()
, leaving the object in a moved-from state.The moved-from object is then used in an operation that has preconditions. Operations that have preconditions include reading the object, dereferencing the object, calling member functions of the object.
Some classes from the standard template library (STL) have a
clear()
method that resets the object to a specified state after a
move. Calling the clear()
method on moved-from objects of these classes
and then reading them is not a defect.
Polyspace does not report this defect on objects of the following classes because their state after a move operation is specified in the C++ standard:
std::unique_ptr
std::shared_ptr
std::weak_ptr
std::unique_ptr
std::basic_ios
std::basic_filebuf
std::thread
std::unique_lock
std::shared_lock
std::promise
std::future
std::shared_future
std::packaged_task
std::vector
Risk
If you use std::move()
on an object, it can be in a valid but
unspecified state. Using moved-from objects in operations that have preconditions can
result in undefined behavior or logical errors.
Fix
To fix this defect, review your code and investigate why your code attempts to use moved-from objects. Reasons for this defect include:
Moving your local objects on their last use, but then later refactoring your code to add additional uses of the local objects. In such cases, either replace the move operation with a copy operation or refactor your code so that use of local object after the move is no longer necessary.
Attempting to use a moved-from container as an empty container. In such cases, clear the container by calling the
clear()
method before you use the container.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
Group: Programming |
Language: C++ |
Default: Off |
Command-Line Syntax:
VAR_READ_AFTER_MOVE
|
Impact: Medium |
Version History
Introduced in R2025a
See Also
Find defects
(-checkers)
| Expensive last use of
variable
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)