This is to rewrite some old homework using STL algorithms instead of hand-written loops and whatnot.
I have a database which vector and lieutenant; Media * & gt; , where media * (among other things) can be a CD, or a book. The database is the only class that handles dynamic memory, and when the program starts, it is formatted a file that is seen below (some simple), while reading the entries, allocating the space, the vector above (v_) Connects to
These objects work as expected when using a hand-written loop: Edit: Sorry, I talked very soon, it is not really a 'hand-written' loop per-click. I was editing the project to remove handwritten edges and it is actually found_if algorithm and a manual Uses extinction, but Rsn is valid until / Edit.
typedef vector & lt; Media * & gt; :: iterator v_iter;
... Zero Database :: removeById (int id) {v_iter this = search_if (v_.begin (), v_.end (), balancer (id)); If (this! = V_.end ()) {delete}; V_.erase (this); }} It should be very self-explanation - if it searches for the id that matches the parameter, and the object is destroyed, it works and Valgrind does not report any memory leaks However, since I would like to use STL, then it should be used to remove the clear-cut reconciliation idiom, which results in something like this below:
This, however, 'works' but according to valgrind causes the memory leak, then what gives? The first version works fine without any leaks - while it always shows 3 alocons 'not empty' for each media object. I = "Text">
This is the reason why you should always, always use smart pointers, the reason is that you have a problem because you use a dumb pointer, it is removed from the vector, but It was not free from the memory that indicated it. Instead, you should use a smart pointer that will always release point-to-memory, where point-to-memory can be freed from the vector.
Comments
Post a Comment