Question about "wild pointer"

Source: Internet
Author: User

Recently I am using QT to build a program, which involves "wild pointer". A problem really arises (why? Because I have read an article by Dr. Lin Rui previously mentioned this problem), it can only be solved after multiple debugging checks. The record is as follows:

Problem description: The purpose of the program is to read the file data, draw a graph on qgraphicsscene, and then delete the graph. Each time the program runs, although qgraphicsscene's clear () is called to clear items, the memory usage increases by about KB.

Problem Analysis: There is a loop in the Program (about 4000 times, depending on the file size). In the loop, I new an object and delete it at the end of the loop. However, the object pointer is not assigned null, resulting in a wild pointer generated for each loop, increasing the memory. The following is a program snippet:

While (! In. atend ())
{
Qpainterpath * plinepath = new qpainterpath ();
Qvector <qpointf> * plinepoint = new qvector <qpointf>;

In> drawitem;
Inname> itemname;

L5roadname. append (itemname );
If (drawitem = "line" | drawitem = "line ")
{
Plinenode = 2;
}
Else if (drawitem = "pline" | drawitem = "pline ")
{
In> plinenode;
In. Flush ();

}
Else
Break;

For (INT I = 0; I! = Plinenode; I ++)
{
In> p1_x> p1_y;
In. Flush ();
Testpoint. setx (p1_x x * 100000000.0 );
Testpoint. sety (20000000000.0-p1_y * 100000000.0 );
Plinepoint-> append (testpoint );

}
For (INT I = 0; I <(plinenode); I ++)
{
If (0 = I)
{
Testpoint1 = plinepoint-> first ();
Plinepath-> moveTo (testpoint1.x (), testpoint1.y ());
}
Else
{
Testpoint1 = plinepoint-> at (I );
Plinepath-> lineto (testpoint1.x (), testpoint1.y ());
}
}

L5roadpath. append (* plinepath );
In> symbol;
Delete plinepoint;
Delete plinepath;
Plinepoint = NULL;
Plinepath = NULL;
}

After the last two statements are added, debug again and find that the memory usage does not increase.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.