gpt4 book ai didi

c++ - Valgrind 无效读取错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:05 25 4
gpt4 key购买 nike

我无法查明我的 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;

我觉得。

关于c++ - Valgrind 无效读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30794887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com