'cppcheck Non-local variable 'xxx' will use pointer to local variable 'config'
This is my code.I'm use cppcheck check my code,error log display Non-local variable 'm_curServer' will use pointer to local variable 'config'.
I am a novice.I don't know what to do about it.I hope to get your help. thanks!
Solution 1:[1]
You are creating a pointer to a function-local variable config which after returning from resetCurServer will be a dangling pointer.
Instead you should just assign by value
class ServerConfigOptDlg {
public:
resetCurServer(ServerConfig config);
private:
ServerConfig m_curServer;
}
void ServerConfigOptDlg::resetCurServer(ServerConfig config) {
m_curServer = config;
}
Or if you need pointer semantics, you should use managed pointers such as std::unique_ptr
#include <memory>
class ServerConfigOptDlg {
private:
resetCurServer(std::unique_ptr<ServerConfig> config);
private:
std::unique_ptr<ServerConfig> m_curServer;
}
void ServerConfigOptDlg::resetCurServer(std::unique_ptr<ServerConfig> config) {
m_curServer = std::move(config);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cory Kramer |
