2011-09-05 1 views
0

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) 

위의 49 번째 줄은 updateServerAgent 코드입니다. 트레이스에서 new 연산자를 볼 수 있지만 updateServerAgent 코드에 새 키워드를 사용하지 않습니다. 필요한 경우 전체 코드를 게시 할 수 있습니다.

코드가

by 0x805FD02: udpclient::communicate() (basic_string.tcc:138) 
by 0x805CAE3: ClientControl::udp_comm_thread(void*) (clientcontrol.cpp:62) 

있다 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)

의해 start_thread 및 과 같은 미량 단지 기능이 다른 또 하나 -

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

+0

이 C입니까? 태그를 C++로 바꾸기 – phoxis

+10

TL; DR .......... –

+1

64 행을 여러 개의 스트림으로 나눕니다. << foo; 명령문을 별도의 행에 표시합니다. 그것은 인수가 경고를주는 단서를 제공해야합니다. –

답변

2

메모리 누수에 대해 : 나는별로 걱정하지 않을 것입니다. 누수가있을 뿐이며 문자열로 누수가 발생했다는보고도 있었지만 설명 할 수는 없었지만 메모리 누출은 발생하지 않았습니다.

잘못된 읽기 정보 : 에이전트가 main()에서 삭제 된 후에도 에이전트를 사용하는 것이 분명합니다. 에이전트는 클라이언트로 전달되지만 클라이언트는 에이전트가 삭제되었음을 알지 못합니다. 이것은 이런 종류의 문제를 일으킬 수 있습니다.

+0

도움 주셔서 감사합니다. 잘못된 읽기에 대해 옳았습니다. – Sterling