0

I declare and assign vector pointer with shared pointer of class that has a big-size array pointer. From deleting the vector pointer, I expect to free the memory from the big array. My question is that why the memory is NOT deallocated at the right moment after 'delete vec' before 'return 0'. From debugging mode, you can see it in the window task manager. Please let me know why 'delete vec' does not work as I expected.

class test
{
public:
    test() { A = new double[500000000]; }
    ~test() { delete[] A; }
    double *A;
};

int main()
{
    vector<shared_ptr<test>> *vec =new vector<shared_ptr<test>>();
    vec->push_back(shared_ptr<test>(new test()));
    vec->push_back(shared_ptr<test>(new test()));
    vec->push_back(shared_ptr<test>(new test()));

    delete vec;

    return 0;
}
Jungwoong
  • 33
  • 7
  • 4
    See https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – juanchopanza Nov 28 '19 at 08:49
  • 3
    There are almost no use-case for pointers to containers. And unless you need polymorphism there often no need for pointers to objects either. So you could use `std::vector vec{ test(), test(), test() };` instead. You could put the vector in a nested scope to emulate the dynamic allocation and deletion of the vector. And it would have the same problem actually ([rule of three/five/zero](https://en.cppreference.com/w/cpp/language/rule_of_three) not being followed). – Some programmer dude Nov 28 '19 at 08:52
  • @Someprogrammerdude I'd say the Threadstarter has exactly one of those rare cases: Large data within the class. If you do not use pointers in that case, the containers have to copy or at least move large amounts of memory. – AlexGeorg Nov 28 '19 at 08:57
  • @Someprogrammerdude no, that is not the same. Your variant makes copies of `test` and OP's code makes copies of `shared_ptr`. I don't really see how his code can result in unreachable pointers (because that's the question, right?). – freakish Nov 28 '19 at 08:57
  • Thanks for your response. I need to use a pointer because I need dynamic memory allocating. The array size may not be that big, anyhow I want dynamic memory allocation. – Jungwoong Nov 28 '19 at 08:58
  • 2
    @Jungwoong Why do you need dynamic memory allocation for 3 pointers (i.e. `std::vector`)? The data buffer of a `std::vector` will _always_ be dynamically allocated, no matter if the `std::vector` variable is on the stack or the heap. – Max Langhof Nov 28 '19 at 09:01
  • 1
    Also, the windows task manager does not really tell you whether your C++ program has memory leaks. When a program requests memory from the OS, it usually doesn't give it back unless it really has to. You are freeing all your data correctly, but that doesn't mean that the memory usage in the task manager has to go down. – Max Langhof Nov 28 '19 at 09:03
  • @Jungwoong how do you measure memory usage? Obviously there is a very small time window between `delete vec;` and your program terminating. I doubt you can accurately catch that moment. It seems that you actually measure some other code, not the one you've shown us. – freakish Nov 28 '19 at 09:03
  • 2
    Your program is absolutely fine! Memory management works as expected. The only problem you have is that you watch and interpret the wrong information from your OS. I really wonder why people give a comment with the rule of three and others do an upvote. As long as your test class will not be copied and moved, it is fine. If you want to store the test class directly in the vector, it must be aware that vector implementation may shift the data around which must be handled. For large arrays it should be made moveable. If so, we have rule of five not three :-) – Klaus Nov 28 '19 at 09:25
  • There is no memory leak in your code. Keep a cout statement in the destruct or of test, see the output. – nayab Nov 28 '19 at 10:11
  • The code is fine and there is no memory leak. When you debug the code, you can see when the memory is free in your window task manager. My question is that why the memory is NOT deallocated at the right moment after 'delete vec' before 'return 0'. – Jungwoong Nov 28 '19 at 10:33
  • 1
    The memory is deallocated, your mistake is expecting task manager to change . This is explained in the linked thread. – M.M Nov 28 '19 at 11:05
  • I got the point, Thank you all and M.M. Best regard! – Jungwoong Nov 28 '19 at 11:33

1 Answers1

0

I tested your code and there is no problem when deleting ptr.

enter image description here

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32