内存泄漏问题

时间:2011-09-05 17:31:39

标签: c++ memory memory-leaks memory-management valgrind

我有一些可能的内存泄漏(根据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

我无法弄清楚的可能泄漏与调用线程的回调或创建stringstreams有关。我在每个线程上都可能出现泄漏,但我会在一个帖子上发布信息。这是痕迹 -

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_thread0x42579F7: 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 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

-

{{1}}

如果有人能帮助我弄清楚/理解这些可能的泄漏,我将非常感激.w

1 个答案:

答案 0 :(得分:2)

关于内存泄漏:我不会太担心。这只是一个可能的泄漏,我们已经看到了有关字符串泄漏的报告,这是我们无法解释的,但这并没有导致内存占用。

关于无效读取:很明显,在main()中删除代理后仍然使用代理。代理程序传递给客户端,但客户端未注意到代理程序已被删除;这可能导致这类问题。