i want to create my files dynamically when i try it this way. i don't get compile errors but my program crashes. could you please point out what i am doing wrong.
The vector holds copies of FooPtr, so it is fine as far as Foo is concerned.
However, we need to also take care of the dynamically allocated std::ofstream objects.
On line 26 you create a shared pointer to the dynamically-allocated ofstream object.
On line 28, you push the shared pointer onto foo_vector, which creates a copy of it, which means that there are now two shared pointers to the ofstream.
At the end of that iteration of the for loop, the first shared pointer falls out of scope, but the one in the vector still exists and still points to the ofstream, so the ofstream is not destroyed.
At the end of the main function, foo_vector falls out of scope, so the final remaining shared pointer to the ofstream is destroyed, so it destroys the ofstream that it's pointing to.
Thus the memory for ofstream is freed only when the last shared pointer to it is destroyed.
The above is true for each ofstream object that you dynamically create in your first for loop.