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 8234239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:24:36+00:00 2026-06-07T18:24:36+00:00

Im using this code to get data from QTcpSocket. I read data in buffer

  • 0

Im using this code to get data from QTcpSocket. I read data in buffer and then if I free it immediately its fine. But if I free it later the application crashes.
QtDocs says that I have to clean up memory with delete[] after using method readBytes

void Widget::slotReadClient()
{
    QTcpSocket* pClientSocket = (QTcpSocket*)sender();
    QDataStream in(pClientSocket);
    in.setVersion(QDataStream::Qt_4_7);
    for (;;)
    {
        if (!m_nNextBlockSize)
        {
            if (pClientSocket->bytesAvailable() < sizeof(quint16))
            {
                break;
            }
            in >> m_nNextBlockSize;
        }
        if (pClientSocket->bytesAvailable() < m_nNextBlockSize)
        {
            break;
        }
      QString attribute;
     quint16 count=0;
      in>>count;//count of massives
     quint16 els=count;
     uint len=0;
     char** wkbs;
     if(count)
          wkbs=new char*[els];
     int j=0;
     char* buf=0;

      while(count)
      {
          in>>attribute;//description its ok
          buf=wkbs[j];
          buf=0;
          in.readBytes(buf,len);
          ui->plainTextEdit->appendPlainText(QString::number(len));//length of buffer ok
          j++;
          count--;
          processGeom(buf);//data is OK
          //delete [] buf; if I use this it works
      }
      ui->plainTextEdit->appendPlainText(QString::number(els));
      j=0;
      while(els)
      {
          buf=wkbs[j];
          delete[] buf;//here I get crash
          els--;
          j++;
      }
      delete[] wkbs;
      m_nNextBlockSize = 0;
    }
}
  • 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-06-07T18:24:38+00:00Added an answer on June 7, 2026 at 6:24 pm

    You are never assigning anything to wkbs, so you are trying to delete[] an uninitialized pointer!

    buf=wkbs[j];
    buf=0;
    in.readBytes(buf,len);
    

    Here, you are assigning to buf three times! The first lines are absolutely pointless, since readBytes will overwrite the value in buf. If you want to keep the allocated buffer, store it in wkbs after readBytes:

    in.readBytes(buf, len);
    wkbs[j] = buf;
    

    Next you are attempting:

    buf=wkbs[j];
    delete[] buf;//here I get crash
    

    With your original code, wkbs[j] is uninitialized, its value could be anything. Deleting this pointer is unsafe.

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

Sidebar

Related Questions

Using this code I am trying to get the data from Templates.plist file but
I am currently using this code to get data from a URL: NSURL *url
I am using this line of code to get two data entries from an
i have this simple code to load data from other php page using get
I'm trying to play video from youtube using this code but I get this
I am using this code to try to get json data from many json
I've been using this code to try and get data from the google weather
I am using this code to read data from Oracle Database. That data stored
Hi I am using this code to get some data from ebay . string
I'm using this code to get the contact list and then I want to

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.