简体   繁体   English

返回值后删除指针

[英]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 我如何使用析构函数释放内存,但遇到运行时错误bad:alloc

The easiest way to fix this, like @BalogPal said, is to stop treating C++ like Java. 像@BalogPal所说的那样,最简单的解决方法是停止像Java那样对待C ++。 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. 我将参数保留为指针的唯一原因是TiXmlElementFirstChildElement()NextSiblingElement()函数返回的结果。 Normally, I would make them references ( TiXmlElement &element ) instead, which is even safer, since you can't pass NULL . 通常,我会改为TiXmlElement &element引用( TiXmlElement &element ),这更加安全,因为您不能传递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. 如果出于性能原因确实需要避免复制,并且编译器不够智能,无法自动执行该操作,则可以使用智能指针 ,这些指针已被引用计数,因此您不必担心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.

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. 将指针用于所需的对象之后,即可进行任何内存管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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