Freeing memory from vector of structs

After I performed some operations on the vector of structs, I need to free the memory. I suppose clear() will not be sufficient, but I'm not sure how to perform individual delete.
1
2
3
4
5
6
7
8
9
10
11
typedef std::vector<VertexRAM> VecVertexRAM; // definition of a vector of structs

template <typename type>
void freeFromMemory(std::vector<type>& myVec) {
    typename std::vector<type>::iterator myIter=myVec.begin();
    while(myIter!=myVec.end()) {
	delete(*myIter);
	++myIter;
    }
    myVec.clear();
}

This outputs the following error:
Code:

error: type ‘struct VertexRAM’ argument given to ‘delete’, expected pointer

Any ideas on how to do this? Thanks
The memory will be freed automatically when the std::vector<> goes out of scope, you don't need to do it yourself.
I need to do it within the function (within which a std::vector<VertexRAM> is not local variable). Any thoughts
Why isn't clear() sufficient?
After calling clear(), size() will be guaranteed to return 0.
Are you running into memory problems? If so I suppose you could ensure that the memory would be freed by using a std::vector pointer and allocating manually. Then pass a reference to the pointer to your function.

1
2
3
4
5
template <typename type>
void freeFromMemory(std::vector<type>*& myVec) {
    delete myVec;
    myVec = new std::vector<type>;
}
Unless the elements of your container are themselves pointers, you don't need to do any freeing. For instance, your code would work on this container, which would need you to do freeing:

typedef std::vector<VertexRAM*> VecVertexRAM;
Topic archived. No new replies allowed.