简体   繁体   中英

delete pointer after return value

hello I have the following functions:

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

I think that I have a memory leak, How can I delete the pointer after returning it? How can I use the destructor to free the memory I am getting a Run-Time error bad:alloc

The easiest way to fix this, like @BalogPal said, is to stop treating C++ like Java. There's no reason to return pointers from any of these functions. Try something like this:

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

The only reason I left the arguments as pointers is that's what your TiXmlElement 's FirstChildElement() and NextSiblingElement() functions return. Normally, I would make them references ( TiXmlElement &element ) instead, which is even safer, since you can't pass NULL .

If you really need to avoid the copying for performance reasons, and your compiler isn't smart enough to do that automatically, you can use smart pointers , which are reference counted, so you don't need to need to worry about delete ing them.

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.

You don't generally "return" pointers. You can pass a pointer into the function as a parameter, and assign a value to it in your function. Since a pointer is a memory location, this value will remain in the pointer when your function returns. You can then do any memory management after you have used the pointer for what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM