CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    MapViewOfFile trouble

    Hi.
    I have a class that load binary file. It has two methods for load the content of files: fread and mapping the file.
    For read content of file with fread:
    Code:
    DWORD CGrafo::CargarGrafoBin(const CString &szNombre) {
        // El grafo binario tiene una cabecera de 256 bytes repartidos de la siguiente manera: 
        // Una estructura ENVOLVENTE (144 bytes)
        // Un unsigned __int32 para el número de Nodos (4 bytes)
        // Un unsigned __int32 para el número de Adyacentes (4 bytes)
        // El resto (104 bytes) lo dejamos a 0 para incorporar nuevas cosas a la cabecera.
    
        if(!PathFileExists(szNombre)) {
            return INVALIDINPUTBINFILE;
        }
    
        IniciarVacio();
    
        CFile fGrafo;
        BOOL bOpen = fGrafo.Open(szNombre, CFile::modeRead|CFile::shareDenyWrite);
    
        if(!bOpen) return CANNOTOPENBINFILE;
    
        BYTE bytesCabecera[256];
        memset(bytesCabecera, 0, 256);
    
        fGrafo.Read(bytesCabecera, 256);    
    
        nNumeroNodos = (unsigned __int32)(*((unsigned __int32 *)(&bytesCabecera[sizeof(ENVOLVENTE)])));
        nNumeroAdyacentes = (unsigned __int32)(*((unsigned __int32 *)(&bytesCabecera[sizeof(ENVOLVENTE) + sizeof(unsigned __int32)])));
        memcpy(&Envolvente, bytesCabecera, sizeof(ENVOLVENTE));
        
        Nodos = (NODO *) malloc(nNumeroNodos * sizeof(NODO));
        Adyacentes = (ADYACENTE *) malloc(nNumeroAdyacentes * sizeof(ADYACENTE));
        vOffsetAdyacentes = (unsigned __int32 *) malloc((nNumeroNodos + 1) * sizeof(unsigned __int32));
    
        fGrafo.Read(Nodos, nNumeroNodos * sizeof(NODO));
        fGrafo.Read(Adyacentes, nNumeroAdyacentes * sizeof(ADYACENTE));
        fGrafo.Read(vOffsetAdyacentes, (nNumeroNodos + 1) * sizeof(unsigned __int32));
    
        fGrafo.Close();    
    
        bMemNodos = true;
        bMemAdyacentes = true;
        bMemOffsetAdy = true;
        
        return OK;
    }
    For mapping the file:

    Code:
    DWORD CGrafo::CargarGrafoMap(const CString &szNombre) {
        if(!PathFileExists(szNombre)) {
            return INVALIDINPUTBINFILE;
        }
        
        hndFile = CreateFile(szNombre, FILE_READ_DATA|FILE_WRITE_DATA, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,NULL);    
        if(hndFile == INVALID_HANDLE_VALUE) return CANNOTOPENBINFILE;
        CString szMappedFileName = szNombre.Mid(szNombre.ReverseFind('\\') + 1, szNombre.GetLength());
        DWORD nSize = GetFileSize(hndFile, NULL);
        hndMappedFile = CreateFileMapping(hndFile, NULL, PAGE_READWRITE, 0, nSize, szMappedFileName);    
        if(hndMappedFile == INVALID_HANDLE_VALUE) return CANNOTCREATEMAPPEDFILE;
        bytView = (BYTE *) MapViewOfFile(hndMappedFile, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, nSize);    
        DWORD dwError = GetLastError();
        if(bytView == NULL) {
            CloseHandle(hndMappedFile);
            CloseHandle(hndFile);
            return CANNOTCREATEVIEWOFMAPPEDFILE;
        }
    
        BYTE bytesCabecera[256];
        memset(bytesCabecera, 0, 256);
    
        memcpy(bytesCabecera, bytView, 256);
    
        nNumeroNodos = (unsigned __int32)(*((unsigned __int32 *)(&bytView[sizeof(ENVOLVENTE)])));
        nNumeroAdyacentes = (unsigned __int32)(*((unsigned __int32 *)(&bytView[sizeof(ENVOLVENTE) + sizeof(unsigned __int32)])));
        memcpy(&Envolvente, bytView, sizeof(ENVOLVENTE));
    
        Nodos = (NODO *)(bytView + 256);
        Adyacentes = (ADYACENTE *)(bytView + 256 + nNumeroNodos * sizeof(NODO));
        vOffsetAdyacentes = (unsigned __int32 *)(bytView + 256 + nNumeroNodos * sizeof(NODO) + nNumeroAdyacentes * sizeof(ADYACENTE));
    
        bMemNodos = true;
        bMemAdyacentes = true;
        bMemOffsetAdy = true;
        bMapped = true;
    
        return OK;
    }
    When I create an array of this class and read the data with fread method it works fine but when the data is read mapping the file some files are loaded ok but after that GetLastError return 8 (from MSDN: ERROR_NOT_ENOUGH_MEMORY) .


    Some idea is appreciated!!!


    Best regards!!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: MapViewOfFile trouble

    Quote Originally Posted by juanpast View Post
    ... When I create an array of this class and read the data with fread method it works fine but when the data is read mapping the file some files are loaded ok but after that GetLastError return 8 (from MSDN: ERROR_NOT_ENOUGH_MEMORY) .


    Some idea is appreciated!!!
    The idea is very simple - increase memorz!

    Besides, there are some other problems in your code:
    1. you are using malloc in VC++ code and apply it so some class that should be treated as a serious mistake.
    2. you don't free the allocated memory
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: MapViewOfFile trouble

    Hi Victor and thanks for your reply.

    The idea is very simple - increase memorz!
    Two methods load the same data but mapping files crashes and using fread not.

    1 you are using malloc in VC++ code and apply it so some class that should be treated as a serious mistake.
    NODO and ADYACENTE are a struct not a class, is necesary use new operator?

    2 you don't free the allocated memory
    Destructor of CGrafo free the memory allocated with malloc, it is not correct?


    Thanks!

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: MapViewOfFile trouble

    Quote Originally Posted by juanpast View Post
    Two methods load the same data but mapping files crashes and using fread not.
    Perhaps, mapping requires more memory?

    Quote Originally Posted by juanpast View Post
    NODO and ADYACENTE are a struct not a class, is necesary use new operator?
    And what is the difference between struct and class?
    There is only one difference: all struct member/methods are public by default while all class member/methods are private by default.

    You may use malloc/free only if your struct is a POD struct, but are you sure it is so now and will be so for ever?

    Quote Originally Posted by juanpast View Post
    Destructor of CGrafo free the memory allocated with malloc, it is not correct?
    No, because your CGrafo::CargarGrafoBin may be called any number of times while the dtor will be called only once.
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: MapViewOfFile trouble

    Very thanks Victor.

    I am doing examples with the same data and with fread works fine but mapping the file not work. It's need to take the same memory. For example:

    Code:
    void CargaGrafos(void) {
        CGrafo vGrafos[5000];
        CString szGrafo = _T("D:\\grafos\\grafo");
        CString szGrafoI;
        for(int i=0;i<5000;i++) {        
            szGrafoI.Format(_T("%s%d"), szGrafo, i);        
            DWORD dRes = vGrafos[i].Open(szGrafoI, 2);
            //DWORD dRes = vGrafos[i].Open(szGrafoI, 1);
        }
    }
    * Open method with second parameter = 1 read the data using
    DWORD CGrafo::CargarGrafoBin(const CString &szNombre)

    ** Open method with second parameter = 2 read the data using
    DWORD CGrafo::CargarGrafoMap(const CString &szNombre)

    Running the example with 1 as parameter works fine but if the parameter is 2 it's not work.



    The other things:

    Besides, there are some other problems in your code:
    1. you are using malloc in VC++ code and apply it so some class that should be treated as a serious mistake.
    2. you don't free the allocated memory
    It's now clear, very thanks.

    Your replies are much appreciated!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured