2013-08-14 3 views
5

dcmtk 라이브러리를 사용하여 다중 프레임 압축 dicom 이미지의 픽셀 데이터를 수정하고 있습니다. 그래서, for 루프의 한 단계에서 나는 각 압축 해제 된 프레임의 픽셀 데이터를 가져 와서 내 소원에 따라 수정하고 각각의 프레임 데이터를 대용량 메모리 버퍼의 각 픽셀 데이터를 연결하려고 시도합니다. for 루프의 핵심 프로세스는 다음과 같습니다.memcpy를 사용하는 중 메모리 오류가 발생 했습니까?

문제는 첫 번째 반복 후 함수의 코드 행에 메모리를 부여하는 것입니다. getUncompressedFrame. 나는 그 줄을 제거 할 때 그 때 오류가없고 전체 for 루프가 절대적으로 잘 작동하는 것처럼 줄이 memcpy(fullBuffer+(i*sizeF),newBuffer,sizeF); 때문에 발생한다고 생각합니다.

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 
    }     
+0

실행이 경우 거기서 멈추고'i'와'sizeF'의 값을 검사하여 그들이 괜찮아 보이는지 살펴 봅니다. 중지되지 않으면 중단 점을 설정하십시오. –

+0

나는 디버거를 실행했고, sizeF는 적절한 숫자를 제공한다. 이 프로그램은 잘 작동하지만 memcpy와 함께 코드 라인을 사용하지는 않는다.하지만 그것을 사용하면 프로그램이 중단된다. –

+0

sizeF의 데이터 유형 ?? –

답변

10

변경이에 fullBuffer의 선언 :

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

귀하의 코드는 배열을 할당하지 않았다, 그것은 값 int(sizeF*numOfFrames) 단일 Uint8을 할당.

+0

예, 큰 실수였습니다. 아! 내 잘못이야. 감사! 나는 지금 시험해 볼 것이다. 나는 내 눈이 나를 속였다고 생각한다. –

+1

그게 문제를 해결했습니다 :). ARGH! 무슨 재앙이야! –

0

getUncompressedFrame이 캐시에 내부 memcpy를 수행하는 경우 메모리가 할당되지 않은 상태에서 널 포인터를 캐시의 인수로 전달할 때 왜 그런지 이해할 수 있습니다.

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

이 (Uint8 먼저 int와 다음을 절단 후) sizeF*numOfFrames 그것의 초기 값을주는 하나의 바이트를 할당한다. 당신은 배열을 원하고, 당신은 int에 크기를 절단하고 싶지 않은 :

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

하거나, 코드에서 가능성이 메모리 누수를 해결하기 위해 : 디버거에서

std::vector<Uint8> fullBuffer(sizeF*numOfFrames); 
관련 문제