Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6539801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:54:08+00:00 2026-05-25T10:54:08+00:00

I am having some possible memory leaks (according to valgrind) and invalid reads. I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:54:08+00:00Added an answer on May 25, 2026 at 10:54 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know it's possible with jQuery, but I'm having some serious troubles with this
I'm doing my best to code against interfaces whenever possible, but I'm having some
As the title states im having some difficulties analzying the average-case memory usage of
I got a linux hardware server having 16GB of physical memory and running some
Having some issues getting my head around the differences between UTF-8, UTF-16, ASCII and
Having some issues with the ... in ObjectiveC. I'm basically wrapping a method and
Having some trouble with what should be a very simple scenario. For example purposes,
Hey having some trouble trying to maintain transparency on a png when i create
Currently having some problems- now = datetime.datetime.now() month = now.strftime(%B) site = wikipedia.getSite('en', 'wikiquote')
Basically having some trouble with using Hover to hide or show an item. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.