Thread: Problem using mmap in windows

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    Angry Problem using mmap in windows

    Hi ,

    I need help. I am facing one issue in mmap usage in code and I am not able to track down. Below is my simplified sample code:


    insert
    Code:
    #include "stdafx.h"
    
    #include <string.h>
    
    #include <windows.h>
    
    #include <iostream>
    
    using namespace std;
    
    #define ISUCCESS 0
    
    #define IFAILURE -1
    
     
    
    const int N=10000;
    
    const int FILESIZE = 10240;
    
    char m_sFileName[25];
    
    int *m_map;
    
    bool m_bIsMapInitzd = false;
    
    #ifdef _WIN32
    
        HANDLE m_fHandle;
    
        HANDLE m_mapHandle;
    
    #endif
    
    void getFileName()
    
    {   
    
          strcpy(m_sFileName,"FileNameMmap.dat");
    
    }
    
     
    
    int InitMMap()
    
    {       
    
        getFileName();
    
    #ifndef _WIN32
    
        /* Open a file for writing.
    
          *     - Creating the file if it doesn't exist.
    
          *     - Truncating it to 0 size if it already exists. (not really needed)
    
          *
    
          * Note: "O_WRONLY" mode is not sufficient when mmaping.
    
          */
    
        m_fd= open(m_sFileName, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
    
        if(m_fd == -1) 
    
        {
    
        //Log Message to signal failure
    
        error_exit("DPStrippedPartition::InitMMap" ,PARTITION_ERR,pmA2U("Error opening file for writing" ));
    
        return IFAILURE;
    
        }
    
        if(lseek(m_fd, FILESIZE - 1, SEEK_SET) == -1)
    
        {
    
            error_exit("DPStrippedPartition::InitMMap" ,PARTITION_ERR,pmA2U("Error in seeking the file" ));
    
            return IFAILURE;
    
        }
    
        if(write(m_fd, "", 1) != 1)
    
        {
    
            error_exit("DPStrippedPartition::InitMMap" ,PARTITION_ERR,pmA2U("Error in writing to the file" ));
    
            return IFAILURE;
    
        }
    
        /* Now the file is ready to be mmapped.
    
          */
    
        m_map = (int*)mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
    
        if (m_map == MAP_FAILED)
    
        {
    
            close(m_fd);
    
                error_exit("DPStrippedPartition::InitMMap" ,PARTITION_ERR,pmA2U("Error mmapping the file"));
    
                return IFAILURE;
    
        }
    
        m_map = new int[FILESIZE];
    
        m_bIsMapInitzd = true;
    
        return ISUCCESS;
    
      #else
    
        m_fHandle = CreateFile(LPCWSTR(m_sFileName),GENERIC_READ|GENERIC_WRITE,
    
            FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    
        if (!m_fHandle) 
    
        {
    
            //Log Message to signal failure
    
            cout<<"Error opening file for writing"<<endl;
    
            return IFAILURE;
    
        }
    
        int buff[1];
    
        buff[0]=1;
    
        DWORD dwBytesWritten;
    
        WriteFile (m_fHandle, buff, 1,
    
                      &dwBytesWritten, NULL);
    
      
    
        m_mapHandle = CreateFileMapping(m_fHandle, NULL, PAGE_READWRITE, 0,FILESIZE, NULL);
    
         
    
        if (!m_mapHandle) 
    
        { 
    
            cout<<"Error creating file mapping"<<endl;
    
            CloseHandle(m_fHandle);
    
            return IFAILURE;
    
        }
    
        m_map = (int*)MapViewOfFile(m_mapHandle, FILE_MAP_ALL_ACCESS,0,0,FILESIZE);
    
        if (!m_map) 
    
        {
    
            cout<<"Error creating map view of the file"<<endl;
    
            CloseHandle(m_mapHandle); 
    
            CloseHandle(m_fHandle); 
    
            return IFAILURE;
    
         }
    
         m_bIsMapInitzd = true;
    
         return ISUCCESS;
    
      #endif
    
    }
    
    int unmap()
    
    {
    
       if(false == m_bIsMapInitzd)
    
       {
    
           return ISUCCESS;//Unmap not required.Just return success
    
       }
    
    #ifndef _WIN32
    
        /* Don't forget to free the mmapped memory
    
           */
    
        if (munmap(m_map, FILESIZE) == -1)
    
        {
    
            error_exit("DPStrippedPartition::unmap" ,PARTITION_WARN,pmA2U("Error unmapping the file"));
    
            /* Decide here whether to close(fd) and exit() or not. Depends... */
    
            return ISUCCESS;//Unmap not required.Just return success
    
        }
    
        /* Un-mmaping doesn't close the file, so we still need to do that.*/
    
        close(m_fd);
    
        delete[] m_map;
    
        m_map = NULL;
    
        m_bIsMapInitzd = false;
    
        return ISUCCESS;
    
    #else
    
        int check= UnmapViewOfFile(m_map);
    
        if(0 == check)
    
        {
    
            //error_exit("DPStrippedPartition::unmap" ,PARTITION_WARN,pmA2U("Error unmapping the file"));
    
            return IFAILURE;
    
        }
    
        CloseHandle(m_mapHandle);
    
        CloseHandle(m_fHandle);
    
        m_bIsMapInitzd = false;
    
        return ISUCCESS;
    
    #endif
    
    }
    
     
    
     
    
    int main()
    
    {
    
          int st = InitMMap();
    
          if(st!=ISUCCESS)
    
          {
    
                cout<<"Error in mapping"<<endl;
    
                return -1;
    
          }
    
          for(int i = 0;i<N;i++)
    
          {
    
                cout<<i<<endl;
    
                m_map[i] = i;ąCrashes here at index 3072
    
          }
    
          for(int i =0;i<N;i++)
    
          {
    
                cout<<m_map[i]<<endl;
    
          }
    
          unmap();
    
          return 0;
    
    }
    Similarly, if I use

    const int N=100000;

    const int FILESIZE = 102400;

    the code crashes at 25600. Do you observe any apparent issue with the code. I am using mmap for the first time.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Below is my simplified sample code:
    Does your double-spaced simplified code ACTUALLY crash?

    If it doesn't, there isn't a lot of point in looking at it.

    Also, leaving a whole mess of code which isn't relevant to your OS (all the stuff which isn't WIN32) just makes it harder to read.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. IPv6 ping in windows...problem..lots of ode:(
    By Neill KElly in forum C Programming
    Replies: 3
    Last Post: 04-27-2009, 11:50 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. Application Termination Problem in Windows XP
    By wasabee in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 12:53 PM
  5. C++ Gurus, UNIX vs. Windows ascii problem perhaps?
    By puck in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2003, 10:33 PM