我目前正在学习/使用 c++,但我来自 Java 背景,所以如果这是一个愚蠢的问题,我深表歉意。下面是一些代码,表示我处理由外部 API 生成的错误的方式。但是,我不确定当我为我的错误处理输出参数赋值时是否会导致内存泄漏。
class ExceptionHandler {
private:
std::string _msg;
int _code;
public:
ExceptionHandler(std::string msg = "", int code = 0) :
_msg(msg),
_code(code)
{
}
int code() {
return _code;
}
std::string msg() {
return _msg;
}
}
//This method returns true if it was executed with no errors
//It returns false if an error occurred
bool foo(ExceptionHandler * errHandler = NULL) {
int sts;
//The API functions return 0 if completed successfully
//else they returns some error code
sts = some_api_func1();
if(sts != 0) { //An error occurred!
if(errHandler) {
ExceptionHandler handler("Error from func1",sts);
*errHandler = handler; //<--- Will this cause a memory leak since I would be losing errHandler's original value??
}
return false;
}
//My motivation for using exception handling this way is that I can
//set the handler's message based on what part it failed at and the
//code associated with it, like below:
sts = some_api_func2();
if(sts != 0) { //An error occurred!
if(errHandler) {
ExceptionHandler handler("Error from func2",sts); //<--- Different err message
*errHandler = handler; //<--- But does this cause a memory leak?
}
return false;
}
return true;
}
//Main method
int main() {
ExceptionHandler handler;
if(!foo(&handler)) {
std::cout << "An exception occurred: (" << handler.code() << ") " << handler.msg() << std::endl;
} else {
std::cout << "Success!" << std::endl;
}
}
如果发生错误,方法 'foo()' 会导致内存泄漏吗?
如果是这样,我该如何解决?如果没有,怎么没有?
这是处理错误的好方法吗?
先感谢您!
编辑
我了解到上面的代码不会产生内存泄漏,但下面的代码是处理错误的更好方法(谢谢大家!):
void foo() {
int sts;
sts = some_api_func1();
if(sts != 0)
throw ExceptionHandler("Error at func1",sts);
sts = some_api_func2();
if(sts != 0)
throw ExceptionHandler("Error at func2",sts);
}
int main() {
try {
foo();
std::cout << "Success!";
} catch(ExceptionHandler &e) { //<--- Catch by reference
std::cout << "Exception: (" << e.code() << ") " << e.msg();
}
}