简体   繁体   English

访问向量元素的成员函数时的访问冲突

[英]Access violation when accessing vector element's member function

I am coding an MFC applcation using VS2012. 我正在使用VS2012编写MFC应用程序。

I have a vector of Bitmap* , I insert elements in a for loop, and then, if I try to access some element's function outside the loop it will give me an access violation. 我有一个Bitmap*的向量,我在for循环中插入了元素,然后,如果我尝试在循环外访问某些元素的函数,则会导致访问冲突。

The strange thing is if I try to access it inside the loop, it works just fine. 奇怪的是,如果我尝试在循环内访问它,它就可以正常工作。

In both examples m_VectorImageNames is already filled with some image files paths, it is not the problem. 在两个示例中, m_VectorImageNames都已经填充了一些图像文件路径,这不是问题。

The following code gives the access violation (at the last line): 以下代码给出了访问冲突(在最后一行):

std::vector<Bitmap *> vectorImages;

for (int i = 0; i < nImages; i++) 
{
    Bitmap img(m_VectorImageNames[i]);

    vectorImages.push_back(&img);
}

int imgWidth= vectorImages[0]->GetWidth();

If I put the GetWidth inside the loop, it returns the correct value: 如果我将GetWidth放入循环中,它将返回正确的值:

std::vector<Bitmap *> vectorImages;

for (int i = 0; i < nImages; i++) 
{
    Bitmap img(m_VectorImageNames[i]);

    vectorImages.push_back(&img);

    int imgWidth= vectorImages[0]->GetWidth();
}

I have tried a few things already, with no success: 我已经尝试了一些方法,但没有成功:

  • Initializing the vector with the size it will have and then inserting each bitmap in its corresponding position (with hope that it was an allocation problem). 使用将要具有的大小初始化向量,然后将每个位图插入其相应位置(希望这是分配问题)。
  • Looping using iterators 使用迭代器循环
  • Making the vector a class member variable 使向量成为类成员变量

Does anyone have a clue of what may be happening? 有谁知道可能会发生什么?

The BitMap object img is defined on the stack inside the loop. BitMap对象img在循环内的堆栈上定义。 The pointer to img is pushed into the vector. 指向img的指针被推入向量。 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. 使用new (用于动态内存分配的运算符)存储位图。

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 . 可能更好的方法(而不是使用原始指针)是使用内存管理的指针,例如std::shared_ptr This depends on your requirements. 这取决于您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问随机向量元素时发生访问冲突 - Access violation when accessing random vector element 在向量中访问指针的成员函数时出现分段错误 - Segmentation fault when accessing a pointer's member function in a vector 使用 static std::vector class 成员时访问冲突 - Access violation when using static std::vector class member 通过atomic :: load()访问vector时发生访问冲突 - Access violation when accessing vector through atomic::load() MFC:在辅助线程中使用对象的成员函数时出现访问冲突错误 - MFC: Getting Access Violation Error when Using an Object's Member Function in a Worker Thread 访问作为类向量的元素的对象的成员函数 - Accessing the member function of an object which is an element of the vector of class 向地图矢量容器的元素分配值时的访问冲突 - Access violation when assigning a value to an element of a map vector container 访问.exe导出函数时,Python ctypes访问冲突 - Python ctypes access violation when accessing an .exe exported function 尝试使用成员函数访问动态分配的成员变量时发生读取访问冲突 - Read Access Violation When Trying to Access Dynamically Allocated Member Variable with Member Function 访问地图时的c ++访问冲突 - c++ Access violation when accessing map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM