泛洪填充算法内存泄漏

本文关键字:泄漏 内存 算法 填充 泛洪 | 更新日期: 2023-09-27 18:37:08

我在某处有内存泄漏。我已经搜索了很多次,对我来说它看起来很可靠。我只是。。不能。。。找到它...好的,背景。这是一个堆栈驱动的洪水填充,这段代码是我向堆栈添加内容的唯一位置。还有更多的代码,所以如果没有人能找到内存泄漏,我会发布更多代码。

这是最奇怪的部分。代码仅使用一种颜色+线条艺术(pictexture)即可正常工作,但是当使用多种颜色并使用填充桶时,我会出现那些奇怪的内存泄漏。

 //descend to the floor
            while(true)
            {
                if(++iterator > total)
                {
                    Debug.Log("broke in the stupid down loop...");
                    break;
                }
                //if we hit line art or a color we're not changing, break out of the loop
                if(PicTexture.GetPixel((int)coords.x, (int)coords.y).a > .5f ||
                   MyTexture.GetPixel((int)coords.x, (int)coords.y) != ColorToChange || coords.y < 0)
                {
                    break;
                }

                //if we're looking right and find an open spot in our texture
                if(reach.right && MyTexture.GetPixel((int)coords.x + 1, (int)coords.y) == ColorToChange
                   && PicTexture.GetPixel((int)coords.x + 1, (int)coords.y).a < .5f)
                {
                    reach.right = false;    //search it and stop looking right
                    if(!search.Contains(new Vector2((int)coords.x + 1, (int)coords.y)))
                        search.Push(new Vector2((int)coords.x + 1, (int)coords.y));
                }
                else
                {
                    if(MyTexture.GetPixel((int)coords.x + 1, (int)coords.y) != ColorToChange
                       || PicTexture.GetPixel((int)coords.x + 1, (int)coords.y).a >= .5f)   //if theres a wall and we're not looking right
                        reach.right = true; //look for an opening to the rightq
                }
                //same thing for left
                if(reach.left && MyTexture.GetPixel((int)coords.x - 1, (int)coords.y) == ColorToChange
                   && PicTexture.GetPixel((int)coords.x - 1, (int)coords.y).a < .5f)
                {
                    reach.left = false;
                    if(!search.Contains(new Vector2((int)coords.x - 1, (int)coords.y)))
                        search.Push(new Vector2((int)coords.x - 1, (int)coords.y));
                }
                else
                {
                    if(MyTexture.GetPixel((int)coords.x - 1, (int)coords.y) != ColorToChange
                       || PicTexture.GetPixel((int)coords.x - 1, (int)coords.y).a >= .5f)
                        reach.left = true;
                }
                MyTexture.SetPixel((int)coords.x, (int)coords.y, BrushColor);
                coords.y--;
            }   

编辑:我刚刚意识到我忘了提到最奇怪的部分。这段代码工作得很好,直到我使用起始颜色(蓝色)以外的颜色。一旦我改变颜色,即使它又变回蓝色,它仍然会破裂。

泛洪填充算法内存泄漏

首先,使用探查器。 我对RedGate的ANTS记忆分析器有过愉快的体验。 当问题不明显时,这确实是获取所需信息的最快方法。

至于你的代码,我乍一看注意到的是,你可能会在很短的时间内创建大量的Vector2对象。 我不知道这是否真的导致了您看到的问题。

顺便说一句,GDI+像狗一样慢。如果您开始注意到性能不佳,您可能需要考虑使用 Bitmap.LockBits 来获取指向内存中图像数据的指针并对其进行操作。 根据我的经验,GDI+ 根本不适合对即使是中等大小的图像进行操作。

我想通了。事实证明,这并不完全是我的错。编写颜色选择器代码的人使用的颜色格式与我不同,但我们使用的引擎有时会隐式地将一种格式转换为另一种格式,这就是为什么它有时仍然有效。非常非常奇怪。谢谢你们的帮助,伙计们!