2015년 8월 21일 금요일

이미지 프로세싱에서 메모리 증가 현상에 대해

라인스캔에서 불러들인 이미지를 뿌려주는 소스에서 계속 3000번 돌리면 죽고, 10K번 돌려도 죽어서 원인을 보니 메모리 증가에 있던 것 같았다. 그래서 VirtualMemory를 할당하고 해제하는 코드에서 문제가 생겼다. 나머지는 CxImage를 써봐도 그렇고,  Pallete를 사용해도 그래서 결국 여기까지 왔다.

While( )
{
 BYTE c = 1;
 BYTE *DataArray = NULL;
// DataArray = (BYTE*)malloc(sizeof(BYTE)*(width * height)*c);
 DataArray = (BYTE*)VirtualAlloc(NULL, sizeof(BYTE)*(width * height)*c,
  MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); //복사될 픽셀데이터 메모리 크기 선언
 BOOL bVL = VirtualLock(DataArray, sizeof(BYTE)*(width * height)*c);  // 4K 씩 증가?????
    memset(DataArray, 255, sizeof(BYTE)*(width * height)*c);
.............


 //free(DataArray);
 //If the dwFreeType parameter is MEM_RELEASE, this parameter must be 0 (zero).
 //The function frees the entire region that is reserved in the initial allocation call to VirtualAlloc 
 VirtualFree(DataArray, sizeof(BYTE)*(width * height)*c, MEM_DECOMMIT); //Memery free
 VirtualFree(DataArray, 0, MEM_RELEASE ); //MEM_DECOMMIT); //Memery free 완전하지 않다.
}
의 루프를 돌려보았다. 먼저.

VirtualFree(p, lSize, MEM_COMMIT);

위의 함수 리턴 값 확인해 보셨습니까? 함수 호출에 실패해서 메모리 해제가 안되는 것 같습니다.
MEM_DECOMMIT 상태는 완전히 메모리가 Release된 상태는 아닙니다.
참고하시기 바랍니다.


if(!VirtualFree(p, lSize, MEM_RELEASE );) {
    AfxMessageBox("Memeory release failed!");
}

이런 식으로 바뀌어야 할 것 같습니다.

또, 올라간다.

MSDN
If the dwFreeType parameter is MEM_RELEASE, this parameter must be 0 (zero). The function frees the entire region that is reserved in the initial allocation call to VirtualAlloc

    VirtualFree(p, 0, MEM_RELEASE);

파라미터를 zero로 두어야 한다고 ㅋㅋ

2015년 8월 7일 금요일