2013-06-19 161 views
1

你好,我有以下功能:刪除指針

Block* Keywords::parseBlock(TiXmlElement* element) 
{ 
    double x1 = atoi(element->Attribute("left")); 
    double y1 = atoi(element->Attribute("top")); 
    double x2 = atoi(element->Attribute("right")); 
    double y2 = atoi(element->Attribute("bottom")); 
    double width = abs(x2 - x1); 
    int bid = atoi(element->Attribute("id")); 

    vector<LineElement*> lines; 
    for (TiXmlElement* sub = element->FirstChildElement("line"); sub; sub = sub->NextSiblingElement("line")) 
     lines.push_back(parseLine(sub)); 

    return new Block(y2,x2,y1,x1,bid,width, lines); 
}///End function parse Block 

LineElement* Keywords::parseLine(TiXmlElement* element) 
{ 
    double x1 = atoi(element->Attribute("left")); 
    double y1 = atof(element->Attribute("top")); 
    double x2 = atoi(element->Attribute("right")); 
    double y2 = atoi(element->Attribute("bottom")); 
    int bid = atoi(element->Attribute("id")); 

    vector<Element*> words; 
    for (TiXmlElement* sub = element->FirstChildElement("word"); sub; sub = sub->NextSiblingElement("word")) 
     words.push_back(parseWord(sub)); 

    return new LineElement(y2,x2,y1,x1,bid,words); 
}///End function parse Line 

Element * Keywords::parseWord(TiXmlElement* element) 
{ 
    string w =element->Attribute("value"); 
    double x1 = atoi(element->Attribute("left")); 
    double y1 = atoi(element->Attribute("top")); 
    double x2 = atoi(element->Attribute("right")); 
    double y2 = atoi(element->Attribute("bottom")); 
    int bid = atoi(element->Attribute("id")); 

    vector<Letter*> chars; 

    for (TiXmlElement* sub = element->FirstChildElement("char"); sub; sub = sub->NextSiblingElement("char")) 
     chars.push_back(parseChar(sub)); 

    return new Element(w,y1, x1, y2,x2,-1,bid,chars); 
}///End function parse word 

Letter * Keywords::parseChar(TiXmlElement* element) 
{ 
    string w = element->Attribute("value"); 
    double x1 = atoi(element->Attribute("left")); 
    double y1 = atoi(element->Attribute("top")); 
    double x2 = atoi(element->Attribute("right")); 
    double y2 = atoi(element->Attribute("bottom")); 
    int bid = atoi(element->Attribute("id")); 
    return new Letter(w,y1,x1,y2,x2,bid); 
} 

我認爲我有內存泄漏,我怎樣才能回國後將其刪除指針? 如何使用析構函數釋放內存我遇到了運行時錯誤:alloc

+0

@vidit不,我沒有錯誤是:通過像'gdb'調試扔「的std :: bad_alloc的」 –

+0

運行此實例,所以你可以計算出它墜毀後終止通話。 –

+0

註冊新類型:wxString 註冊新類型:STL字符串 註冊新類型:STL矢量 設置斷點 調試器名稱和版本:GNU GDB(GDB)7.5 子進程PID:6316 程序接收信號SIGTRAP,跟蹤/斷點陷阱。 (c:\ Windows \ system32 \ ntdll.dll) #15 0x0043d519 in __gnu_cxx :: new_allocator :: deallocate(this = 0x28f684,__p = 0xa90460)在c:/ mingw/bin /中。 ./lib/gcc/mingw32/4.6.2/include/c++/ext/new_allocator.h:98 C:包括\ C++ \分機\ new_allocator.h:98:3034:BEG:0x43d519 在C:\ MinGW的\ LIB \ GCC \的mingw32 \ 4.6.2 \包括\ C++ \分機\ new_allocator.h:98 –

回答

6

解決這個問題的最簡單的方法,像@BalogPal說,就是停止治療C++像Java。沒有理由返回任何這些函數的指針。嘗試是這樣的:

Block Keywords::parseBlock(TiXmlElement* element) 
{ 
    double x1 = atoi(element->Attribute("left")); 
    double y1 = atoi(element->Attribute("top")); 
    double x2 = atoi(element->Attribute("right")); 
    double y2 = atoi(element->Attribute("bottom")); 
    double width = abs(x2 - x1); 
    int bid = atoi(element->Attribute("id")); 

    vector<LineElement> lines; 
    for (TiXmlElement* sub = element->FirstChildElement("line"); sub; sub = sub->NextSiblingElement("line")) 
     lines.push_back(parseLine(sub)); 

    return Block(y2, x2, y1, x1, bid, width, lines); 
} 

LineElement Keywords::parseLine(TiXmlElement* element) 
{ 
    double x1 = atoi(element->Attribute("left")); 
    double y1 = atof(element->Attribute("top")); 
    double x2 = atoi(element->Attribute("right")); 
    double y2 = atoi(element->Attribute("bottom")); 
    int bid = atoi(element->Attribute("id")); 

    vector<Element> words; 
    for (TiXmlElement* sub = element->FirstChildElement("word"); sub; sub = sub->NextSiblingElement("word")) 
     words.push_back(parseWord(sub)); 

    return LineElement(y2, x2, y1, x1, bid, words); 
} 

Element Keywords::parseWord(TiXmlElement* element) 
{ 
    string w = element->Attribute("value"); 
    double x1 = atoi(element->Attribute("left")); 
    double y1 = atoi(element->Attribute("top")); 
    double x2 = atoi(element->Attribute("right")); 
    double y2 = atoi(element->Attribute("bottom")); 
    int bid = atoi(element->Attribute("id")); 

    vector<Letter> chars; 

    for (TiXmlElement* sub = element->FirstChildElement("char"); sub; sub = sub->NextSiblingElement("char")) 
     chars.push_back(parseChar(sub)); 

    return Element(w, y1, x1, y2, x2, -1, bid, chars); 
} 

Letter Keywords::parseChar(TiXmlElement* element) 
{ 
    string w = element->Attribute("value"); 
    double x1 = atoi(element->Attribute("left")); 
    double y1 = atoi(element->Attribute("top")); 
    double x2 = atoi(element->Attribute("right")); 
    double y2 = atoi(element->Attribute("bottom")); 
    int bid = atoi(element->Attribute("id")); 
    return Letter(w, y1, x1, y2, x2, bid); 
} 

我剩餘的參數作爲指針的唯一原因是,這就是你的TiXmlElementFirstChildElement()NextSiblingElement()函數返回。通常情況下,我會讓它們的引用(TiXmlElement &element)來代替,這是更安全的,因爲你無法通過NULL

如果您出於性能原因確實需要避免複製,並且您的編譯器不夠智能,無法自動執行該操作,則可以使用smart pointers,它們是引用計數的,因此您無需擔心delete

std::shared_pointer<Block> Keywords::parseBlock(TiXmlElement* element) 
{ 
    double x1 = atoi(element->Attribute("left")); 
    double y1 = atoi(element->Attribute("top")); 
    double x2 = atoi(element->Attribute("right")); 
    double y2 = atoi(element->Attribute("bottom")); 
    double width = abs(x2 - x1); 
    int bid = atoi(element->Attribute("id")); 

    vector<std::shared_pointer<LineElement> > lines; 
    for (TiXmlElement* sub = element->FirstChildElement("line"); sub; sub = sub->NextSiblingElement("line")) 
     lines.push_back(parseLine(sub)); 

    return std::shared_pointer<Block>(new Block(y2, x2, y1, x1, bid, width, lines)); 
} 

// etc. 
+0

最簡單的方法,是的!但是,如果創建/複製這些類很昂貴,智能指針會給出更好的'java等效', –

+0

「沒有理由」?似乎你從未使用過大的XML文檔;) – Pragmateek

+1

@ g-makulik看起來你忘記了移動語義和NRVO。 –

-1

您通常不會「返回」指針。您可以將一個指針作爲參數傳遞給該函數,並在函數中爲其分配一個值。由於指針是內存位置,當函數返回時,該值將保留在指針中。您可以在使用完指針後進行任何內存管理。

+3

返回指針沒有什麼不尋常的。我認爲你的方法沒有任何優勢,只會讓事情變得更加複雜。 – Beta

+0

我想,教育我的人必須是一個宗教的東西。在返回指針時總會對誰擁有內存感到困惑,因此,誰負責清理。 – eli