开发者

heap corruption when deconstructing sf::Image array

开发者_Go百科

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;
}


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜