關於“野指標”的問題

來源:互聯網
上載者:User

最近在用Qt做一個程式,其中就涉及到了“野指標”的,果然出了問題(為什麼要用果然呢? 因為以前看過林銳博士的一篇文章提到過這個問題),多次調試檢查後才解決。現記錄如下:

問題描述:程式的目的大概是一個讀檔案資料並在QGraphicsScene上畫出圖形再刪掉這些圖形。程式每運行一次,雖然調用QGraphicsScene的clear()清除items,但是記憶體佔用增加大概300K左右。

問題分析:程式中有一個迴圈(大概4000次左右,根據檔案大小不同而不同),在迴圈中我new了一個對象,在迴圈結束時delete了這個對象。 但是並沒有把對象指標賦為NULL,導致每迴圈一遍產生了一個野指標,從而記憶體不斷增加。以下是程式片段:

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 * 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; 
                        }

 在增加了最後兩條語句後再調試,發現記憶體佔用不再增長。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.