0

I'm reading through Scott Meyers Effective C++ and I have a question regarding resource management. In the book, he suggests that one should use the auto_ptr object to manage memory as it will automatically call the destructor of the object it points to when it goes out of scope. Here is the example provided:

void f() {
    Investment *pInv = createInvestment();    // Call factory function
    ...                                       // use pInv
    delete pInv;                              // release object
}

He claims that this is potentially problematic because there could be a number of things that happen in the ... that would prevent the method from reaching delete pInv (premature return statement, thrown exception, ...). This is the code that he suggests in its place

void f() {
    std::auto_ptr<Investment> pInv(createInvestment()); // call factory function
    ...                                                 // use pInv as before
}                                                       // automatically delete pInv

This works because no matter what happens pInv will be deleted when it goes out of scope.

My question is this: why wouldn't the destructor of pInv be called when it goes out of scope in the first code block. Wouldn't a premature return statement push the object out of scope, or is that not how garbage collection works in C++?

Hopefully I have explained my confusion well enough. Thanks in advance!

wakey
  • 2,283
  • 4
  • 31
  • 58
  • 5
    *"is that not how garbage collection works in C++?"* In C++, there is no garbage collection. – Cornstalks May 28 '16 at 15:28
  • 2
    `pInv` is a scalar type, not a class type, so it doesn't have a destructor. The lifetime of `pInv` still ends at the end of the scope of course, but there's nothing to do. – Kerrek SB May 28 '16 at 15:30
  • So is the automatic deletion of auto_ptr when it goes out of scope a feature that is limited to only auto_ptr? – wakey May 28 '16 at 15:30
  • `one should use the auto_ptr object to manage memory` No. – uh oh somebody needs a pupper May 28 '16 at 15:32
  • @wKavey: What, why? No. All class types have destructors that are run at the end of object lifetime. – Kerrek SB May 28 '16 at 15:32
  • Are you actually confused about what the type of `pInv` is? – Kerrek SB May 28 '16 at 15:33
  • 1
    Please don't use `auto_ptr` in modern C++. Use `unique_ptr` or `shared_ptr`. – Jesper Juhl May 28 '16 at 15:33
  • @KerrekSB: I might understand now(?): Because (in the first example) `pInv` is a pointer to an object the pointer is destroyed when leaving scope but the `Investment` object remains allocated somewhere - while in the second example `pInv` is an `auto_ptr` which frees both itself and the `Investment` object it points to upon leaving scope? – wakey May 28 '16 at 15:40
  • 1
    Effective C++ is an *excellent* book, so it's good that you're reading it. But, as people have pointed out here, it is a bit out of date. It refers to the C++03 standard, which has been effectively superseded by the C++11 standard. In C++03, you used `std::auto_ptr` as a smart pointer class to automatically manage the lifetime of objects being pointed to by pointers (such as those you create with `new`). However, in C++11, you use `std::unique_ptr` or `std::shared_ptr`. Exact same concept, though. I believe that the answers to the duplicate question will clear up your confusion. – Cody Gray - on strike May 28 '16 at 15:40
  • 1
    @wKavey: That's kind of getting there, yes. In the second example, the destructor of the class-type object executes at the end of the object's lifetime and can thus perform arbitrary, user-defined actions. `auto_ptr` is designed so that the destructor destroys the dynamically allocated object that the autoptr is holding. – Kerrek SB May 28 '16 at 15:47

0 Answers0