• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

Out of Memory Error

Hi Experts
             My program is showing out of memory error when it has to execute large files. I am not able to figure out why it is happening
ROOT_CLUSTER_LIST *ptTempClusterInfo,*ptCurrentClusterInfo;
      LARGE_INTEGER      lrMovePos ={0},
            lrCurrentPos ={0};
      lrMovePos.QuadPart =i64fatposition;
      SetFilePointerEx(hBackupFile,lrMovePos ,0,FILE_BEGIN);
      if(dwClusterNumber<2)
      {
            return;
      }
      if((m_BackupParameter._bPartitionType ==11)||
            (m_BackupParameter._bPartitionType ==12))
      {
            BYTE *pBuff=new BYTE[headerFAT._dwSPF*SECTSIZE];
            DWORD dwByteRead=0;
            ptTempClusterInfo=(ROOT_CLUSTER_LIST *)calloc(1,sizeof(ROOT_CLUSTER_LIST));
            ptTempClusterInfo->next=NULL;
            ptTempClusterInfo->dwClusterNumber =dwClusterNumber;            
            if(*stRootClusterStart==NULL)
            {
                  *stRootClusterStart=ptTempClusterInfo;
                  ptCurrentClusterInfo=ptTempClusterInfo;
                  dwCountRootClusterNode=dwCountRootClusterNode+1;
            }
            ReadFile(hBackupFile,pBuff,headerFAT._dwSPF*SECTSIZE,&dwByteRead,NULL);
            do
            {
                  DWORD dwRootNextCluster=0;
                  DWORD dwSectorNum=dwClusterNumber/128;
                  if(dwSectorNum==0)
                  {
                        __int64 i64toteloffset=(dwClusterNumber)*4;
                        dwRootNextCluster =*((DWORD*)(pBuff+i64toteloffset));
                  }
                  else
                  {
                        DWORD dwremainclusterentry=dwClusterNumber-(dwSectorNum*128);
                        __int64 i64toteloffset=dwSectorNum*SECTSIZE+(dwremainclusterentry)*4;
                        if(i64toteloffset< (headerFAT._dwSPF *SECTSIZE))
                        {
                              dwRootNextCluster =*((DWORD*)(pBuff+i64toteloffset));
                        }
                        else
                        {
                              break;
                        }
                  }
                  if(dwRootNextCluster!=0x0FFFFFFF)
                  {
                        ptTempClusterInfo=(ROOT_CLUSTER_LIST *)calloc(1,sizeof(ROOT_CLUSTER_LIST));
                        ptTempClusterInfo->next =NULL;
                        ptTempClusterInfo->dwClusterNumber =dwRootNextCluster;
                        ptCurrentClusterInfo->next =ptTempClusterInfo;
                        ptCurrentClusterInfo=ptTempClusterInfo;
                        dwClusterNumber=dwRootNextCluster;
                        dwCountRootClusterNode++;
                  }
                  else
                  {

                        break;
                  }
            }while(true);
            delete []pBuff;
            pBuff=NULL;
      }
      else
      {
            BYTE *pBuff=new BYTE[5*1024*1024];
            DWORD dwByteRead=0;
            ptTempClusterInfo=(ROOT_CLUSTER_LIST *)calloc(1,sizeof(ROOT_CLUSTER_LIST));
            ptTempClusterInfo->next=NULL;
            ptTempClusterInfo->dwClusterNumber =dwClusterNumber;
            if(*stRootClusterStart==NULL)
            {
                  *stRootClusterStart=ptTempClusterInfo;
                  ptCurrentClusterInfo=ptTempClusterInfo;
                  
            }
            ReadFile(hBackupFile,pBuff,5*1024*1024,&dwByteRead,NULL);
            do
            {
                  DWORD dwRootNextCluster=0;
                  DWORD dwSectorNum=dwClusterNumber/256;
                  if(dwSectorNum==0)
                  {
                        __int64 i64toteloffset=(dwClusterNumber)*2;
                        dwRootNextCluster =*((WORD*)(pBuff+i64toteloffset));
                  }
                  else
                  {
                        DWORD dwremainclusterentry=dwClusterNumber-(dwSectorNum*256);
                        __int64 i64toteloffset=dwSectorNum*SECTSIZE+(dwremainclusterentry)*2;
                        if(i64toteloffset< (5 * 1024 * 1024))
                        {
                              dwRootNextCluster =*((WORD*)(pBuff+i64toteloffset));
                        }
                        else
                        {
                              break;
                        }
                  }

                  if((dwRootNextCluster!=65535)&&(dwRootNextCluster!=0))
                  {
                        ptTempClusterInfo=(ROOT_CLUSTER_LIST *)calloc(1,sizeof(ROOT_CLUSTER_LIST));
                        ptTempClusterInfo->next =NULL;
                        ptTempClusterInfo->dwClusterNumber =dwRootNextCluster;
                        ptCurrentClusterInfo->next =ptTempClusterInfo;
                        ptCurrentClusterInfo=ptTempClusterInfo;
                        dwClusterNumber=dwRootNextCluster;
                        dwCountRootClusterNode++;
                  }
                  else
                  {
                        break;
                  }
            }while(true);
            delete []pBuff;
            pBuff=NULL;
      }
0
davinder101
Asked:
davinder101
1 Solution
 
FarWestCommented:
you mean larger than 5MB?
0
 
davinder101Author Commented:

No.It is executing for files means if in a drive there is  2Gb of data then its going out of memory error.
0
 
FarWestCommented:
then maybe it is 32/64 bit issue
I think you can try to use  __int64 instead of LARGE_INTEGER & DWORD
0
 
AndyAinscowCommented:
At which line, and how much memory do you require at that line?
0
 
sarabandeCommented:
why do you use both 'new' and 'calloc' ?

why don't you ever free the ptTempClusterInfo ?

why do you have only one ReadFile call and where (and how) do you handle the big files?

if you were using statements like

__int64 i64toteloffset=dwSectorNum*SECTSIZE+(dwremainclusterentry)*2;

Open in new window


it doesn't help that you have a 64-bit integer at the left side when the value at the right side can be greater than 32-bit max integer cause all variables on the right side are 32-bit. then a 32-bit temporary was used at the right side. to avoid that you can do instead

__int64 i64toteloffset=dwSectorNum;
i64toteloffset *= SECTSIZE;
i64toteloffset += (dwremainclusterentry)*2;

Open in new window


Sara
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now