解构 sf::Image 数组时堆损坏

发布于 2024-11-30 20:35:08 字数 1689 浏览 0 评论 0原文

所以我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2024-12-07 20:35:08

有一些事情可以帮助您缩小原因范围:

  • 堆损坏很少发生在程序崩溃时,这使得它们很难追踪。它可能与崩溃点的对象有关,或者可能是另一个对象/代码损坏了它。
  • 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:

  • A heap corruption rarely occurs at the point when the program crashes which makes them hard to track down. It may be related to the object at the crash point or it may have been another object/code that corrupted it.
  • In CreateTransition() you pass the animationArray[] by value but you then pass it into a thread procedure. The lifetime of animationArray[] ends when you return from CreateTransition() which means if the thread procedure runs after this point its void* ptr parameter will not point to a valid object. You do have a thread.Wait() in the current code but also a comment about removing it. Either pass animationArray[] 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.
  • Consider using a std::vector<sf::Image> instead of an array.
  • Make sure you understand and implement the Rule of Three for sf::image and any dependent classes (like MyPixels). Not doing this can result in double-frees, leaked memory, and heap corruption like you are seeing.
  • If all else fails try to duplicate the issue in a temporary test project and reduce it to the smallest amount of code possible. Eliminate members of sf::image one at a time until the problem goes away. Similarly, delete lines from CreateTransition() 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文