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.