-1

enter image description here 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!

1 Answers1

0

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);
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218