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

时间:2011-08-19 00:52:05

标签: c++ image pixel sfml

所以我试图在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;
}

1 个答案:

答案 0 :(得分:0)

一些可能有助于缩小原因的事情:

  • 程序崩溃时很少发生堆损坏,这使得难以跟踪。它可能与崩溃点处的对象有关,也可能是另一个损坏它的对象/代码。
  • CreateTransition()中,您按animationArray[]传递值,但随后将其传递给线程过程。从animationArray[]返回时CreateTransition()的生命周期结束,这意味着如果线程过程在此点之后运行,则其void* ptr参数将不会指向有效对象。您在当前代码中有thread.Wait(),但也有关于删除它的注释。要么通过引用传递animationArray[],除非有特定原因没有,或者为线程过程创建临时副本以确保它对有效对象进行操作。
  • 考虑使用std::vector<sf::Image>而不是数组。
  • 确保您了解并实施sf::image MyPixels和任何相关类(如sf::image)。不这样做会导致双重释放,内存泄漏和堆损坏,就像你看到的那样。
  • 如果所有其他方法都失败,请尝试在临时测试项目中复制该问题,并将其减少到可能的最小代码量。逐个消除CreateTransition()个成员,直到问题消失。同样,从{{1}}中删除行,并从线程过程中删除其他行。您最终会得到一些触发问题的特定行或一个空项目。
相关问题