2

On C++ primer 5 edition. chapter 12 Dynamic Memory:

Consider the following function that operates on a shared_ptr:

// ptr is created and initialized when process is called
void process(shared_ptr<int> ptr)
{
// use ptr
}   // ptr goes out of scope and is destroyed

The parameter to process is passed by value, so the argument to process is copied into ptr. Copying a shared_ptr increments its reference count. Thus, inside process the count is at least 2. When process completes, the reference count of ptr is decremented but cannot go to zero. Therefore, when the local variable ptr is destroyed, the memory to which ptr points will not be deleted. The right way to use this function is to pass it a shared_ptr:

shared_ptr<int> p(new int(42)); // reference count is 1
process(p); // copying p increments its count; in process the reference count is 2
int i = *p; // ok: reference count is 1

It is said that use_count in process is at least 2 thus when ptr goes out of scope it is destructed but the memory of object it manages is not freed?!

I think it is logically but consider this:

void process(shared_ptr<int> sp) {
    cout << sp.use_count() << endl;
    cout << *sp << endl;    
}

int main(){
    process(shared_ptr<int>(new int(57)));
}

The output:

1 and it is not 2 as he said?

57

  • Does this mean some std::move was applied by the compiler as an optimization?
  • Does he mean a memory-leak will happen in his example if no optimization was applied by the compiler to remove the Copy?
curiousguy
  • 8,038
  • 2
  • 40
  • 58
Itachi Uchiwa
  • 3,044
  • 12
  • 26
  • 3
    Likely, neither copy nor move was performed, but was [elided](https://en.cppreference.com/w/cpp/language/copy_elision) entirely. – Igor Tandetnik Oct 26 '19 at 23:22
  • @IgorTandetnik: Yes I missed it! Copy-elision. – Itachi Uchiwa Oct 26 '19 at 23:23
  • @IgorTandetnik: So does it mean if running the same code without `copy-elision` optimization then is that code yields a memory leak? – Itachi Uchiwa Oct 26 '19 at 23:24
  • 2
    There can't be a memory leak no matter how many times it's copied because it will always be deleted when the last instance goes out of scope. – Paul Evans Oct 26 '19 at 23:29
  • 2
    It is important to understand how `shared_ptr` works, but no part of that understanding requires one to keep track of `use_count`. That's `shared_ptr`'s job. `use_count` is available for you merely for convenience, perhaps logging and diagnostic purposes. But insofar as ordinary C++ code goes, it has no need to worry about `use_count`. That's `shared_ptr`'s job. – Sam Varshavchik Oct 26 '19 at 23:30
  • @PaulEvans: I aggree with you. As long as the last `shared_ptr` goes out of scope will handle the deletion of memory it manages. So the use_count inside `process` can be 2 or 1? – Itachi Uchiwa Oct 26 '19 at 23:31
  • `use_count` of 1 just means you have the only instance of owning smart ptr for some resource; the definition of a ref counting (RC) owning is that the release function (by default, `delete`) is called with decrementing the RC produces a 0. – curiousguy Oct 26 '19 at 23:31
  • @ItachiUchiwa A function accepting a *shared* owning smart ptr accepts the idea it doesn't have the sole owner, so why would the function care about the ref count? – curiousguy Oct 26 '19 at 23:33
  • Yes, because if the use count is 1 there's only one instance that will go out of scope at the end of `process` and a use count of 2 means there's a copy in `process` and the first instance will go out if scope at the end of `main`. – Paul Evans Oct 26 '19 at 23:35
  • @SamVarshavchik: Do you think this is a mistake in the book? "When process completes, the reference count of ptr is decremented but cannot go to zero. Therefore, when the local variable ptr is destroyed, the memory to which ptr points will not be deleted." – Itachi Uchiwa Oct 27 '19 at 10:56
  • @curiousguy: Is it a mistake in the book? becauase the book says: the memory will not be deleted which points to memory leak? – Itachi Uchiwa Oct 27 '19 at 10:57
  • @PaulEvans: Do you consider this a mistake in the book? – Itachi Uchiwa Oct 27 '19 at 10:58
  • Why marked as Duplicate? But in fact it may show a mistake in the book? I am not to ask about Optimization like Copy-elision? – Itachi Uchiwa Oct 27 '19 at 11:02
  • No, it's not a "mistake in the book". The book is simply not considering the case where the function parameter is a temporary object. – Sam Varshavchik Oct 27 '19 at 12:50
  • @SamVarshavchik: "// ptr is created and initialized when process is called" but I think this is a temporary he means. – Itachi Uchiwa Oct 27 '19 at 13:15
  • 2
    Yes for sure it can be a mistake in the book. One thing to consider about shared_ptr s is they are really safe and the last one of them will manage the destruction of the underlying object. It doesn't matter how many shared_ptr manage your dynamic object or whether an optimization or not has been applied, use the in the right contest and it is guaranteed to you no memory leak. – Raindrop7 Oct 27 '19 at 18:26
  • 1
    @SamVarshavchik: Maybe the OP is quite correct because I've re-read the paragraph and the writer told: "ptr is created and initialized when process is called" which means a temporary shared_ptr is created upon `process` call. Also the writer told " Therefore, when the local variable ptr is destroyed, the memory to which ptr points will not be deleted. The right way to use this function is to pass it a shared_ptr:" which he means that Memory-leak also then he said: "The right way..." which means that the previous example is incorrect although it is correct. – Raindrop7 Oct 27 '19 at 18:30

0 Answers0