解构 sf::Image 数组时堆损坏
所以我试图在 SFML 中为 sf::Image 制作淡入淡出过渡动画,但遇到了一个小问题。
当我没有注释掉下面调用的函数时,当解构图像时,我会在 main()
末尾收到错误消息
“Windows 已触发断点。这可能是由于损坏 堆。”
的行包含代码 GLCheck(glDeleteTextures(1, &Texture));
为什么会发生这种情况,为什么只有在运行 CreateTransition() 时?
还有一点要注意:当我注释掉 aray[I] = aray[0]
时,中断不会发生不会发生。
我发布了下面的功能。
void CreateTransition(sf::Image& start, sf::Image animationArray[numbImgs]){
animationArray[0] = start;
void threadFunc(void* imgArray);
sf::Thread thread(threadFunc, reinterpret_cast<void*>(animationArray));
thread.Launch();
thread.Wait(); // comment this out once I get the code working
}
void threadFunc(void* ptr){
sf::Image* aray = reinterpret_cast<sf::Image*> (ptr);
sf::Color filter(0, 0, 0, 5);
for(int I= 1; I< numbImgs; I++){
//aray[I].Copy(aray[0], 0, 0, sf::IntRect(0, 0, 0, 0), true);
aray[I] = aray[0]; // error doesn't occur when commented out
RecolorImage(aray[I], filter);
}
}
Image& Image::operator =(const Image& Other)
{
Image Temp(Other);
std::swap(myWidth, Temp.myWidth);
std::swap(myHeight, Temp.myHeight);
std::swap(myTextureWidth, Temp.myTextureWidth);
std::swap(myTextureHeight, Temp.myTextureHeight);
std::swap(myTexture, Temp.myTexture);
std::swap(myIsSmooth, Temp.myIsSmooth);
std::swap(myNeedArrayUpdate, Temp.myNeedArrayUpdate);
std::swap(myNeedTextureUpdate, Temp.myNeedTextureUpdate);
myPixels.swap(Temp.myPixels);
return *this;
}
so I'm trying to make a fade transition animation for an sf::Image in SFML, and I'm have a small problem.
When I don't comment out the function called below, I get an error at the end of main()
when the images are being deconstructed saying
"Windows has triggered a breakpoint. This may be due to a corruption
of the heap."
The line this happens on contains the code GLCheck(glDeleteTextures(1, &Texture));
Why would this be happening, and why only when CreateTransition() is run?
One more note: when I comment out aray[I] = aray[0]
the break doesn't occur.
I posted the function below.
void CreateTransition(sf::Image& start, sf::Image animationArray[numbImgs]){
animationArray[0] = start;
void threadFunc(void* imgArray);
sf::Thread thread(threadFunc, reinterpret_cast<void*>(animationArray));
thread.Launch();
thread.Wait(); // comment this out once I get the code working
}
void threadFunc(void* ptr){
sf::Image* aray = reinterpret_cast<sf::Image*> (ptr);
sf::Color filter(0, 0, 0, 5);
for(int I= 1; I< numbImgs; I++){
//aray[I].Copy(aray[0], 0, 0, sf::IntRect(0, 0, 0, 0), true);
aray[I] = aray[0]; // error doesn't occur when commented out
RecolorImage(aray[I], filter);
}
}
Image& Image::operator =(const Image& Other)
{
Image Temp(Other);
std::swap(myWidth, Temp.myWidth);
std::swap(myHeight, Temp.myHeight);
std::swap(myTextureWidth, Temp.myTextureWidth);
std::swap(myTextureHeight, Temp.myTextureHeight);
std::swap(myTexture, Temp.myTexture);
std::swap(myIsSmooth, Temp.myIsSmooth);
std::swap(myNeedArrayUpdate, Temp.myNeedArrayUpdate);
std::swap(myNeedTextureUpdate, Temp.myNeedTextureUpdate);
myPixels.swap(Temp.myPixels);
return *this;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些事情可以帮助您缩小原因范围:
CreateTransition()
中,您按值传递animationArray[]
,然后将其传递到线程过程中。当您从 CreateTransition() 返回时,animationArray[] 的生命周期结束,这意味着如果线程过程在此点之后运行其void* ptr
参数不会指向有效的对象。当前代码中确实有一个thread.Wait()
,但也有一个关于删除它的注释。要么通过引用传递animationArray[],除非有特定原因不这样做,要么为线程过程创建一个临时副本以确保它在有效对象上运行。std::vector
而不是数组。的三法则 sf::image
和任何依赖类(例如MyPixels
)。不这样做可能会导致双重释放、内存泄漏和堆损坏,就像您所看到的那样。sf::image
成员,直到问题消失。同样,删除 CreateTransition() 中的行以及线程过程中的其他行。您最终会得到一些触发问题的非常具体的行,或者得到一个空项目。A few things which might help you narrow down the cause:
CreateTransition()
you pass theanimationArray[]
by value but you then pass it into a thread procedure. The lifetime ofanimationArray[]
ends when you return fromCreateTransition()
which means if the thread procedure runs after this point itsvoid* ptr
parameter will not point to a valid object. You do have athread.Wait()
in the current code but also a comment about removing it. Either passanimationArray[]
by reference unless there is a specific reason not to, or create a temporary copy for the thread procedure to ensure it operates on valid objects.std::vector<sf::Image>
instead of an array.sf::image
and any dependent classes (likeMyPixels
). Not doing this can result in double-frees, leaked memory, and heap corruption like you are seeing.sf::image
one at a time until the problem goes away. Similarly, delete lines fromCreateTransition()
and other lines from the thread procedure. You'll either end up with a few very specific lines that trigger the issue or an empty project.