[英]Char* related memory leak
我正在运行Valgrind以检查我的代码是否存在内存泄漏。 Valgrind没有显示任何泄漏发生,但是我有一段代码,我相信应该导致泄漏,并且我不明白变量是如何被清除的,或者Valgrind没有捕获到。 为什么两个char *数组不创建泄漏?
void BasicEngine::ConnectionInput(int ConnectionId, const char* ClientInput)
{
// find client assignment to this ConnectionId
Client* thisClient = this->ClientFind(ConnectionId);
int SpaceLocation = strcspn(ClientInput," ");
char* verb;
char* args;
if(SpaceLocation == strlen(ClientInput))
{
verb = (char*)ClientInput;
args = (char*)"";
}
else
{
verb = new char[SpaceLocation+1];
args = new char[strlen(ClientInput)-SpaceLocation+1];
sscanf(ClientInput,"%s %[^\n]",verb,args);
}
if(thisClient != NULL)
{
// ... client is always null, this is not being reached at the moment.
}
else
{
if(this->refCmdHandler != NULL)
if(this->refCmdHandler->cmdHandler(ConnectionId,ClientInput))
return;
}
this->refServer->TransmitNL(ConnectionId,"Invalid Command.");
}
bool BasicCmdProc::cmdHandler(int ConnectionId, string ClientInput)
{
Transmit(ConnectionId,string("You Said: ") + ClientInput);
return true;
}
如果我输入“ hello”
输出是:您说的:你好
并且没有发现泄漏。
hello
包含空格,因此strcspn
返回strlen(ClientInput)
,因此您进入了第一个分支。 在该分支中, verb
和args
不会动态分配,因此不会泄漏。
但是请注意,在“可能已分配”的内存中放置一个变量点通常非常危险,因为很难确定是否应释放该变量。 因此,您应该在两个分支中都使用new
,并在末尾无条件释放两个变量。 或者,更好的是,使用std::string
并完全避免此问题。
输入为hello
时,两个char *
元素verbs
或args
均未分配,因为:
int SpaceLocation = strcspn(ClientInput," ");
char* verb;
char* args;
if (SpaceLocation == strlen(ClientInput))
{
verb = (char*)ClientInput;
args = (char*)"";
}
else
{
verb = new char[SpaceLocation+1];
args = new char[strlen(ClientInput)-SpaceLocation+1];
sscanf(ClientInput,"%s %[^\n]",verb,args);
}
strcspn(ClientInput, " ")
(又名SpaceLocation
strcspn(ClientInput, " ")
的输出与strlen(ClientInput)
相同,因此不会执行new[]
操作,也不会分配内存。
您如何判断是否需要释放verb
和args
? 不知道是否释放内存很危险。
为什么两个char *数组不创建泄漏?
它们仅在您将new
运算符的结果分配给它们时才起作用(在这种情况下,Valgrind应该通知您)。 如果为常量字符串分配常量字符串,则不会有内存泄漏-常量字符串在程序的整个生命周期中都有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.