繁体   English   中英

Valgrind无效读取错误

[英]Valgrind Invalid Read Error

我无法查明我的Valgrind错误的确切原因:

==3868== Invalid read of size 2
==3868==    at 0x100001F1F: Shrinker::Execute() (in ./proj4B)
==3868==    by 0x1000029CD: Filter::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)
==3868==    by 0x10000299E: Filter::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)
==3868==    by 0x10000299E: Filter::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)
==3868==    by 0x10000299E: Filter::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)
==3868==    by 0x10000299E: Filter::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)
==3868==    by 0x10000299E: Filter::Update() (in ./proj4B)
==3868==  Address 0x100c12040 is 0 bytes inside a block of size 7,201,152 free'd
==3868==    at 0x10000D94F: free (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==3868==    by 0x100001BD5: PNMreader::Execute() (in ./proj4B)
==3868==    by 0x100001954: Source::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)
==3868==    by 0x10000299E: Filter::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)
==3868==    by 0x10000299E: Filter::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)
==3868==    by 0x10000299E: Filter::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)
==3868==    by 0x10000299E: Filter::Update() (in ./proj4B)
==3868==    by 0x100001819: Image::Update() const (in ./proj4B)

我在想这是由于Shrinker :: Execute()中的嵌套循环和/或它的析构函数:

void Shrinker::Execute() {
    int inWidth = 0, inHeight = 0, maxVal = 255;
    int halfWidth = 0, halfHeight = 0;

    inWidth = img1->GetWidth();
    inHeight = img1->GetHeight();

    halfWidth = inWidth / 2;
    halfHeight = inHeight / 2;

    buffer = (Pixel *) malloc(sizeof(Pixel)*halfWidth*halfHeight);

    int in = 0, out = 0;
    for (int i = 0; i < halfHeight; i++) {
        for (int j = 0; j < halfWidth; j++) {
            in = i*2*inWidth+j*2;
            out = i*halfWidth+j;
            buffer[out] = img1->GetPixels()[in];
        }
    }

    img.ResetSize(halfWidth, halfHeight);
    img.SetMaxVal(maxVal);
    img.SetPixels(buffer);

}   // end Shrinker::Execute()

我试图对嵌套循环和Shrinker中的malloc进行所有可能的细微调整,但均无济于事。 它的析构函数释放缓冲区并将其设置为NULL。 任何指导将不胜感激。

实际上,我不知道您的代码的目的。 但是,也许您在以下代码中使用了错误的参数:

 in = i*2*inWidth+j*2; 

它应该是:

 in = i*2*halfwidth+j*2; 

我认为。

我在这里猜测,但是由于它访问了先前已释放的块-某些东西正在破坏Shrinker::Execute分配的缓冲区-如果唯一释放的地方是您的析构函数,那么您您的Shrinker可能创建了错误的副本(可能是临时副本)-例如,如果您从函数返回Shrinker对象,就会发生这种情况。

您需要确保已阻止创建副本或正确实现了副本构造函数和赋值运算符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM