The BitMap object img
is defined on the stack inside the loop. The pointer to img
is pushed into the vector. Then the memory storage that the pointer elements point to in the vector is lost once the loop has terminated.
Use new
(the operator for dynamic memory allocation) to store the bitmaps.
for (int i = 0; i < nImages; i++)
{
Bitmap *img = new BitMap(m_VectorImageNames[i]);
vectorImages.push_back(img);
...
}
Possibly better (instead of using raw pointers) would be to use a memory-managed pointer such as std::shared_ptr
. This depends on your requirements.