¿Por qué no se llama al destructor en los objetos que está devolviendo?

So as I understand it you can return objects in C++ by returning pointers to them. But I was under the impression that once a function is finished running, the destructor gets called on all the objects. Why doesn't the destructor get called on the object you're returning?

preguntado el 09 de marzo de 12 a las 23:03

6 Respuestas

Only the destructors of objects with duración del almacenamiento automático are called when those objects leave their scope (not just a function, but any scope: braces, for-statements, even single-line expressions).

On the other hand, objects of estático storage duration are only destroyed at program exit, and objects of lugar de trabajo dinámico storage duration (i.e. those created with a new operator) only get destroyed manually on tu solicitud.

When you return a pointer in the way you describe, then almost surely that pointer points to a dynamically created object, and thus it is the responsibility of the recipient of the pointer to see to it that the object is eventually cleaned up. This is the great disadvantage of naked pointers: They do not convey any implicit ownership statement, and you have to provide the information about who's responsible for the dynamic object manually outside the code.

respondido 09 mar '12, 23:03

The destructor for an object you created with new doesn't get called until you delete el puntero.

To make sure you don't forget to delete the pointer, try using smart pointers such as std::shared_ptr or std::unique_ptr.

If your compiler isn't new enough to include smart pointers, you can find some from Boost.

respondido 09 mar '12, 23:03

When you return the reference to the object it is no longer scoped to the function and therefore does not die with the function (ie the pointer to the object does not get deleted).

respondido 09 mar '12, 23:03

That would only happen if you wrote code incorrectly. For example:

Foo* MyFunction()
    Foo foo(2);
    return &foo;
} // foo is implicitly destroyed as we return

This is broken. I take the address of foo, but it is also destroyed because it goes out of scope. This is fine:

Foo* MyFunction()
    Foo* j=new Foo(2);
    return j;
} // j is implicitly destroyed as we return

This is fine. While j is destroyed because it goes out of scope, the value that I returned is still the address of the Foo I created. The foo that I allocated will not be destroyed because it doesn't go out of scope.

respondido 09 mar '12, 23:03

Es posible que desee mencionar static Foo foo(2); return &foo; guión. - SigTerm

Why doesn't the Foo object go out of scope in the second example? - me encanta stackoverflow

@badatmath: The Foo* objetoj) goes out of scope (and is destroyed because it has automatic storage duration), but the Foo creado por new Foo(2) has dynamic storage duration (and is only destroyed when you explicitly destroy it (with delete)). The Foo creado por new Foo(2) is never really in a scope (it lives out in the ether on the free store), so it can't go out of scope. - Mankarse

@badatmath: What name would go out of scope? The only name created inside the scope of the function is f. - David Schwartz

@DavidSchwartz: but I thought the object was created inside the scope too? Or does the destructor thing only apply to variables that were actually declared (like j as opposed to the object it points to)? - me encanta stackoverflow

There are two ways of allocating objects: the stack and the heap.

1) Objects are created on the montón con new keyword. These objects are not destroyed until they are deleted.

2) Other objects exist on the montón - Sin newEn delete. These objects are destroyed when they go out of scope. If you return a pointer to one of these objects (by taking the address of a stack-allocated object, the pointer will be invalid once the object has gone out of scope.

respondido 09 mar '12, 23:03

C++ (outside of .net) does not delete an object until you tell it to.

respondido 09 mar '12, 23:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.