Yes, you're leaking memory.
Whenever you allocate memory with the SysAllocString
family, you must either free it using SysFreeString
or pass it to something that will take responsibility for freeing it. The VARIANT type does not clean up its own memory.
You have a couple of options for fixing it:
Use CComVariant
or variant_t
. It provides an operator=, copy constructors and a destructor that manage the memory for you. The drawback to storing them in a vector is that temporary copies will be created and destroyed (the same as if you'd stored std::string). This is the easiest, and my preferred, solution.
Call SysFreeString
on every string in vec_MyVec
when you're finished. This is more efficient but also much more error prone and difficult to do correctly, especially when considering exception safety.
Store a vector of std::tr1::shared_ptr<CComVariant>
, this will prevent temporary copies being created, but you'll have the overhead of reference counting instead.