使用memcpy时出现内存错误?

5
我正在使用dcmtk库来修改多帧压缩dicom图像的像素数据。所以,在一个`for`循环的某个阶段,我获取每个解压后的帧的像素数据并根据我的意愿进行修改,并尝试逐帧将每个修改后的像素数据连接在一起形成一个大的内存缓冲区。该`for`循环的核心过程如下。
问题是在第一次迭代之后,当我调用`getUncompressedFrame`函数时,代码行处会分配内存。我认为这是因为我在这一行中使用了`memcpy(fullBuffer+(i*sizeF),newBuffer,sizeF)`,因为当我删除该行时,在那个时候就没有错误,整个`for`循环也运行得非常好。
请问我在使用`memcpy`时是否犯了错误?谢谢。
Uint32 sizeF=828072;// I just wrote it to show what is the data type. 
Uint8 * fullBuffer = new Uint8(int(sizeF*numOfFrames));//The big memory buffer
for(int i=0;i<numOfFrames;i++)
{
    Uint8 * buffer = new Uint8[int(sizeF)];//Buffer for each frame
    Uint8 * newBuffer = new Uint8[int(sizeF)];//Buffer in which the modified frame data is stored 
    DcmFileCache * cache=NULL;
    OFCondition cond=element->getUncompressedFrame(dataset,i,startFragment,buffer,sizeF,decompressedColorModel,cache);
    //I get the uncompressed individual frame pixel data 
    if(buffer != NULL)
    {
        for(unsigned long y = 0; y < rows; y++)
        {
            for(unsigned long x = 0; x < cols; x++)
            {
                if(planarConfiguration==0)
                {
                    if(x>xmin && x<xmax && y>ymin && y<ymax)
                    {
                        index=(x + y +  y*(cols-1))*samplePerPixel;
                        if(index<sizeF-2)
                        {
                            newBuffer[index]  = 0;
                            newBuffer[index + 1]  = 0;
                            newBuffer[index +2]  = 0;
                        }
                    }
                    else
                    {
                        index=(x + y +  y*(cols-1))*samplePerPixel;
                        if(index<sizeF-2)
                        {
                            newBuffer[index]  = buffer[index];
                            newBuffer[index + 1]  = buffer[index + 1];
                            newBuffer[index + 2]  = buffer[index + 2];
                        }
                    }
                }
            }
        }
        memcpy(fullBuffer+(i*sizeF),newBuffer,sizeF);
        //concatenate the modified frame by frame pixel data
    }                   

在调试器中运行,如果它在那儿停止了,那么检查isizeF的值是否正确。如果它没有停止,那么设置一个断点。 - Some programmer dude
我已经运行了调试器,i和sizeF给出了适当的数字。程序运行良好,但如果我使用memcpy这行代码,程序就会崩溃。 - the_naive
那个嵌套的x y循环的目的是什么?顺便说一句,你可以用'index=(x + y*(cols))samplePerPixel;'代替'index=(x + y + y(cols-1))*samplePerPixel;',为什么要用前者呢? - Lior
Uint32 sizeF = 0; 你在分配大小为0的空间? - aah134
7
请不要“修复”问题中的错误。这会使得无法将您的原始代码与答案中的建议进行比较。 - Cody Gray
显示剩余2条评论
3个回答

10

fullBuffer的声明更改为以下内容:

Uint8 * fullBuffer = new Uint8[int(sizeF*numOfFrames)];

你的代码没有分配一个数组,它只分配了一个单独的 Uint8 ,其值为 int(sizeF*numOfFrames)


是的,那是一个非常大的错误,啊!我的错。谢谢!我现在会尝试。我想我的眼睛欺骗了我。 - the_naive

3
Uint8 * fullBuffer = new Uint8(int(sizeF*numOfFrames));

这将分配一个单字节,给它一个初始值为sizeF*numOfFrames(首先将其截断为int,然后再截断为Uint8)。你需要一个数组,并且不想将大小截断为int

Uint8 * fullBuffer = new Uint8[sizeF*numOfFrames];
                              ^                 ^

或者,为了修复您代码中可能存在的内存泄漏问题:
std::vector<Uint8> fullBuffer(sizeF*numOfFrames);

0
如果方法getUncompressedFrame正在执行内部memcpy以进行缓存,那么传递空指针作为缓存参数是没有分配内存的,这就解释了为什么会出现这种情况。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接