删除返回值后的指针

delete pointer after return value

本文关键字:指针 返回值 删除      更新时间:2023-10-16

hello我有以下函数:

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

我认为我有一个内存泄漏,我怎么能删除指针返回后?我如何使用析构函数来释放内存,我得到一个运行时错误坏:

解决这个问题最简单的方法,就像@BalogPal说的,就是停止像对待Java那样对待c++。没有理由从这些函数返回指针。试试这样做:

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

如果出于性能原因你确实需要避免复制,而你的编译器没有足够的智能来自动做到这一点,你可以使用智能指针,它是引用计数的,所以你不需要担心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.

一般不"返回"指针。您可以将指针作为参数传递给函数,并在函数中为其赋值。由于指针是一个内存位置,当函数返回时,这个值将保留在指针中。然后,您可以在使用指针后进行任何内存管理。