内存泄漏问题
我遇到了一些可能的内存泄漏(根据 valgrind)和无效读取。我希望有人能帮助我理解为什么会发生这种情况。
首先,我收到无效的读取,并且跟踪导致我将值放入字符串流中。这是跟踪 -
Thread 4:
Invalid read of size 4
at 0x80586AB: TcpClient::updateServerAgent() (tcpclient.cpp:64)
by 0x805CB15: ClientControl::update_server_thread(void*) (clientcontrol.cpp:49)
by 0x4040E98: start_thread (pthread_create.c:304)
by 0x43C873D: clone (clone.S:130)
Address 0x4553290 is 24 bytes inside a block of size 52 free'd
at 0x4025907: operator delete(void*) (vg_replace_malloc.c:387)
by 0x804F0A0: main (main.cpp:191)
我将发布这些的代码块。 Main
-
//make agent and set robot's agent
Agent* agent = new Agent(g, robot, 'e');
robot.setAgent(agent);
//make initial start and goal positions
Position start(1,1);
Position end(1,1);
agent->setPosition(start);
agent->setGoal(end);
//set initial path
//Position goal = agent->getGoal();
Path p = agent->traverse(agent->getGoal());
agent->setPath(p);
client.setIP(args[3]);
u_client.setIP(args[3]);
//launch the clients
if(client.launchClient() && u_client.launch_client()) {
cout<<"\nSuccessful Connection!";
//set robot id
agent->getRobot()->setID(args[4][0]);
//set the agents
client.setAgent(agent);
u_client.setAgent(agent);
//set client control's members
cc.setClient(&client);
cc.setUDP(&u_client);
//go
cc.control();
robot.pauseSensorStream();
delete agent; //************LINE 191***************
} //end if successful connection
} //end if client
ClientControl -
inline void ClientControl::update_server_thread_i() {
for(;;) {
usleep(UPDATE_SERVER_TIME);
myClient->updateServerAgent(); //***********LINE 49**************
} //end while
}
updateSeverAgent -
void TcpClient::updateServerAgent() {
//hold message to get the length of it
std::stringstream messagelength;
//message is 1 prow pcol grow gcol sensorhigh sensorlow
//************LINE 64 IS THE NEXT LINE OF MESSAGELENGTH<<...*******************
messagelength<<"1 "<<myAgent->getPosition().getRow()<<" "<<myAgent->getPosition().getCol()<<" "<<myAgent->getGoal().getRow()<<" "<<myAgent->getGoal().getCol();
//make it into a string
std::string tempStrLen = messagelength.str();
int length_of_rest = 3;
//find number of digits in prow
while(isdigit(tempStrLen[length_of_rest]))
length_of_rest++;
//****repeat that process a few times**
//create message to send to server
std::stringstream message;
message<<"@ "<<messagelength.str(); //*****note I don't get an issue here****
//std::cout<<"\nmessage: "<<message.str();
//send
int numSent = send(fd, message.str().c_str(), message.str().length(), 0);
} //END UPDATESERVERAGENT
我无法弄清楚可能的泄漏与调用线程的回调或与创建字符串流有关。我在每个线程上都可能出现泄漏,但我只会在一个线程上发布信息。这是跟踪 -
22 Bytes in 1 blocks are possibly lost in loss record 3 of 12
at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
by 0x42579F7: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
by 0x805880C: TcpClient::updateServerAgent() (basic_string.tcc:138)
by 0x805CB15: ClientControl::update_server_thread(void*) (clientcontrol.cpp:49)
by 0x4040E98: start_thread (pthread_create.c:304)
by 0x43C873T: clone (clone.S:130)
updateServerAgent 代码在上面,并注明了第 49 行。我在跟踪中看到了 new 运算符,但我的 updateServerAgent 代码中从未有 new 关键字。如果需要的话我可以发布整个代码。
另一种具有相同的跟踪,只是 start_thread
和之间的函数不同 通过 0x42579F7: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator
--
by 0x805FD02: udpclient::communicate() (basic_string.tcc:138)
by 0x805CAE3: ClientControl::udp_comm_thread(void*) (clientcontrol.cpp:62)
代码是 -
inline void ClientControl::udp_comm_thread_i() {
myUDP->communicate(); //*****LINE 62*******
}
如果
void udpclient::communicate() {
//message to send
std::ostringstream tosend;
//hold return value of sendto
int numSent;
while(1) {
//sleep
usleep(15000);
//reset tosend
tosend.str("");
//grab sensor values
Sensor_Packet temp = myAgent->getRobot()->getSensorValue(myAgent->getRobot()->getCurrentSensor());
//put header onto tosend and concatenate the values
tosend<<"@ "<<myAgent->getRobot()->getID()<<" "<<temp.values[1]<<" "<<temp.values[0];
//send
numSent = sendto(fd, tosend.str().c_str(), tosend.str().length(), 0, servinfo->ai_addr, servinfo->ai_addrlen);
if(numSent < 0)
printf("\nError sending %m", errno);
//else
// cout<<"\nUDP Sent: "<<tosend.str();
} //end while
} //END COMMUNICATE
有人能帮助我找出/理解这些可能的泄漏,我会非常感谢.w
I am having some possible memory leaks (according to valgrind) and invalid reads. I was hoping someone could help me understand why they are happening.
First I am getting invalid read's and the trace leads up to me putting values into a stringstream. Here is the trace -
Thread 4:
Invalid read of size 4
at 0x80586AB: TcpClient::updateServerAgent() (tcpclient.cpp:64)
by 0x805CB15: ClientControl::update_server_thread(void*) (clientcontrol.cpp:49)
by 0x4040E98: start_thread (pthread_create.c:304)
by 0x43C873D: clone (clone.S:130)
Address 0x4553290 is 24 bytes inside a block of size 52 free'd
at 0x4025907: operator delete(void*) (vg_replace_malloc.c:387)
by 0x804F0A0: main (main.cpp:191)
I'll post the blocks of code for these. Main
-
//make agent and set robot's agent
Agent* agent = new Agent(g, robot, 'e');
robot.setAgent(agent);
//make initial start and goal positions
Position start(1,1);
Position end(1,1);
agent->setPosition(start);
agent->setGoal(end);
//set initial path
//Position goal = agent->getGoal();
Path p = agent->traverse(agent->getGoal());
agent->setPath(p);
client.setIP(args[3]);
u_client.setIP(args[3]);
//launch the clients
if(client.launchClient() && u_client.launch_client()) {
cout<<"\nSuccessful Connection!";
//set robot id
agent->getRobot()->setID(args[4][0]);
//set the agents
client.setAgent(agent);
u_client.setAgent(agent);
//set client control's members
cc.setClient(&client);
cc.setUDP(&u_client);
//go
cc.control();
robot.pauseSensorStream();
delete agent; //************LINE 191***************
} //end if successful connection
} //end if client
ClientControl -
inline void ClientControl::update_server_thread_i() {
for(;;) {
usleep(UPDATE_SERVER_TIME);
myClient->updateServerAgent(); //***********LINE 49**************
} //end while
}
updateSeverAgent -
void TcpClient::updateServerAgent() {
//hold message to get the length of it
std::stringstream messagelength;
//message is 1 prow pcol grow gcol sensorhigh sensorlow
//************LINE 64 IS THE NEXT LINE OF MESSAGELENGTH<<...*******************
messagelength<<"1 "<<myAgent->getPosition().getRow()<<" "<<myAgent->getPosition().getCol()<<" "<<myAgent->getGoal().getRow()<<" "<<myAgent->getGoal().getCol();
//make it into a string
std::string tempStrLen = messagelength.str();
int length_of_rest = 3;
//find number of digits in prow
while(isdigit(tempStrLen[length_of_rest]))
length_of_rest++;
//****repeat that process a few times**
//create message to send to server
std::stringstream message;
message<<"@ "<<messagelength.str(); //*****note I don't get an issue here****
//std::cout<<"\nmessage: "<<message.str();
//send
int numSent = send(fd, message.str().c_str(), message.str().length(), 0);
} //END UPDATESERVERAGENT
A possible leak I can't figure out has to do with either calling a thread's callback or something to do with creating stringstreams. I get a possible leak on each thread, but I'll just post the info on one. Here is the trace -
22 Bytes in 1 blocks are possibly lost in loss record 3 of 12
at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
by 0x42579F7: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
by 0x805880C: TcpClient::updateServerAgent() (basic_string.tcc:138)
by 0x805CB15: ClientControl::update_server_thread(void*) (clientcontrol.cpp:49)
by 0x4040E98: start_thread (pthread_create.c:304)
by 0x43C873T: clone (clone.S:130)
The updateServerAgent code is above with line 49 noted. I see the operator new in the trace, but I never have the new keyword in my updateServerAgent code. I can post the whole code if needed.
Another one with the same trace just different functions between start_thread
and
by 0x42579F7: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
which are
by 0x805FD02: udpclient::communicate() (basic_string.tcc:138)
by 0x805CAE3: ClientControl::udp_comm_thread(void*) (clientcontrol.cpp:62)
The code is -
inline void ClientControl::udp_comm_thread_i() {
myUDP->communicate(); //*****LINE 62*******
}
--
void udpclient::communicate() {
//message to send
std::ostringstream tosend;
//hold return value of sendto
int numSent;
while(1) {
//sleep
usleep(15000);
//reset tosend
tosend.str("");
//grab sensor values
Sensor_Packet temp = myAgent->getRobot()->getSensorValue(myAgent->getRobot()->getCurrentSensor());
//put header onto tosend and concatenate the values
tosend<<"@ "<<myAgent->getRobot()->getID()<<" "<<temp.values[1]<<" "<<temp.values[0];
//send
numSent = sendto(fd, tosend.str().c_str(), tosend.str().length(), 0, servinfo->ai_addr, servinfo->ai_addrlen);
if(numSent < 0)
printf("\nError sending %m", errno);
//else
// cout<<"\nUDP Sent: "<<tosend.str();
} //end while
} //END COMMUNICATE
If anyone could help me figure out/understand these possible leaks I would be very grateful.w
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于内存泄漏:我不会太担心。这只是一个可能的泄漏,我们也看到了字符串泄漏的报告,我们无法解释,但这并没有导致内存占用。
关于无效读:很明显,在main()中删除代理后,您仍然使用该代理。 Agent被传递给客户端,但客户端没有注意到Agent被删除;这可能会导致此类问题。
About the memory leak: I wouldn't worry too much. It's only a possible leak and we have seen reports of leaks with strings too, which we couldn't explain, but that didn't lead to memory hogging.
About the invalid read: it's obvious that you still use the agent after it was deleted in main(). Agent is passed to client, but client was not noticed that agent was deleted; this can lead this sort of problems.