Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. memset() run time error

memset() run time error

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manju 3
    wrote on last edited by
    #1

    Hi all, I am getting some "Access violation writing " for memset for every 30 min after i run my application. And the cursor goes to the below line:

    memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));

    Here is the function i am using i am trying,and this function i am calling in timer:

    #define BUF_SIZE 2048
    const int max_msg = 2048;
    char *pSharedBuffer2 = (char*)malloc(max_msg);
    int WriteToShared()
    {

    	HANDLE handleMappedFile;
    	LPVOID lpMsgBuf;
    
    	handleMappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL, PAGE\_READWRITE, HIWORD(BUF\_SIZE), LOWORD(BUF\_SIZE), szName4);
    	if (handleMappedFile == NULL)
    	{
    		printf("Could not create file mapping object.\\n");
    		LocalFree( lpMsgBuf );
    		
    	}
    	memset(pSharedBuffer2, 0, sizeof(\*pSharedBuffer2));
    
    	pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE);
    	
    	
    	if (pSharedBuffer2 == NULL)
    	{
    		printf("Could not map view of file.\\n");
    		CloseHandle(handleMappedFile);
    		
    	} 
    	
    
    sprintf(i,"%d",Test);	
    addMsg1(i, pSharedBuffer2);
    
    return 0;
    

    OnTimer()
    {
    WriteToShared()
    }

    }

    Can anyone guide,whats exactly going wrong. Thanks Sharan

    B C S 3 Replies Last reply
    0
    • M manju 3

      Hi all, I am getting some "Access violation writing " for memset for every 30 min after i run my application. And the cursor goes to the below line:

      memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));

      Here is the function i am using i am trying,and this function i am calling in timer:

      #define BUF_SIZE 2048
      const int max_msg = 2048;
      char *pSharedBuffer2 = (char*)malloc(max_msg);
      int WriteToShared()
      {

      	HANDLE handleMappedFile;
      	LPVOID lpMsgBuf;
      
      	handleMappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL, PAGE\_READWRITE, HIWORD(BUF\_SIZE), LOWORD(BUF\_SIZE), szName4);
      	if (handleMappedFile == NULL)
      	{
      		printf("Could not create file mapping object.\\n");
      		LocalFree( lpMsgBuf );
      		
      	}
      	memset(pSharedBuffer2, 0, sizeof(\*pSharedBuffer2));
      
      	pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE);
      	
      	
      	if (pSharedBuffer2 == NULL)
      	{
      		printf("Could not map view of file.\\n");
      		CloseHandle(handleMappedFile);
      		
      	} 
      	
      
      sprintf(i,"%d",Test);	
      addMsg1(i, pSharedBuffer2);
      
      return 0;
      

      OnTimer()
      {
      WriteToShared()
      }

      }

      Can anyone guide,whats exactly going wrong. Thanks Sharan

      B Offline
      B Offline
      Benjamin Bruno
      wrote on last edited by
      #2

      may be give memset(pSharedBuffer2, 0, max_msg );

      1 Reply Last reply
      0
      • M manju 3

        Hi all, I am getting some "Access violation writing " for memset for every 30 min after i run my application. And the cursor goes to the below line:

        memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));

        Here is the function i am using i am trying,and this function i am calling in timer:

        #define BUF_SIZE 2048
        const int max_msg = 2048;
        char *pSharedBuffer2 = (char*)malloc(max_msg);
        int WriteToShared()
        {

        	HANDLE handleMappedFile;
        	LPVOID lpMsgBuf;
        
        	handleMappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL, PAGE\_READWRITE, HIWORD(BUF\_SIZE), LOWORD(BUF\_SIZE), szName4);
        	if (handleMappedFile == NULL)
        	{
        		printf("Could not create file mapping object.\\n");
        		LocalFree( lpMsgBuf );
        		
        	}
        	memset(pSharedBuffer2, 0, sizeof(\*pSharedBuffer2));
        
        	pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE);
        	
        	
        	if (pSharedBuffer2 == NULL)
        	{
        		printf("Could not map view of file.\\n");
        		CloseHandle(handleMappedFile);
        		
        	} 
        	
        
        sprintf(i,"%d",Test);	
        addMsg1(i, pSharedBuffer2);
        
        return 0;
        

        OnTimer()
        {
        WriteToShared()
        }

        }

        Can anyone guide,whats exactly going wrong. Thanks Sharan

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        manju 3 wrote:

        memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));

        That is probably NOT what you intended to do, because it sets just 1 byte (sizeof(char) is ONE). Did you mean

        memset(pSharedBuffer2, 0, max_msg);

        ?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        M 1 Reply Last reply
        0
        • C CPallini

          manju 3 wrote:

          memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));

          That is probably NOT what you intended to do, because it sets just 1 byte (sizeof(char) is ONE). Did you mean

          memset(pSharedBuffer2, 0, max_msg);

          ?

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          M Offline
          M Offline
          manju 3
          wrote on last edited by
          #4

          Hi, Thanks for your reply.Even i tried with

          memset(pSharedBuffer2, 0, max_msg);

          but getting the same "access violation for writing" Thanks Sharan

          C C L 3 Replies Last reply
          0
          • M manju 3

            Hi, Thanks for your reply.Even i tried with

            memset(pSharedBuffer2, 0, max_msg);

            but getting the same "access violation for writing" Thanks Sharan

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            What is i (i.e. how is it declared/defined?)?

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            M 1 Reply Last reply
            0
            • M manju 3

              Hi, Thanks for your reply.Even i tried with

              memset(pSharedBuffer2, 0, max_msg);

              but getting the same "access violation for writing" Thanks Sharan

              C Offline
              C Offline
              Chuck OToole
              wrote on last edited by
              #6

              and what is addMsg1() and what does it do? It is blowing up in there? Also, you have a macro that defines to 2048 and max_msg that is set to 2048 and then use both in the code, interchangably. Pick one and use it exclusively otherwise you'll get bit one of these days.

              1 Reply Last reply
              0
              • M manju 3

                Hi all, I am getting some "Access violation writing " for memset for every 30 min after i run my application. And the cursor goes to the below line:

                memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));

                Here is the function i am using i am trying,and this function i am calling in timer:

                #define BUF_SIZE 2048
                const int max_msg = 2048;
                char *pSharedBuffer2 = (char*)malloc(max_msg);
                int WriteToShared()
                {

                	HANDLE handleMappedFile;
                	LPVOID lpMsgBuf;
                
                	handleMappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL, PAGE\_READWRITE, HIWORD(BUF\_SIZE), LOWORD(BUF\_SIZE), szName4);
                	if (handleMappedFile == NULL)
                	{
                		printf("Could not create file mapping object.\\n");
                		LocalFree( lpMsgBuf );
                		
                	}
                	memset(pSharedBuffer2, 0, sizeof(\*pSharedBuffer2));
                
                	pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE);
                	
                	
                	if (pSharedBuffer2 == NULL)
                	{
                		printf("Could not map view of file.\\n");
                		CloseHandle(handleMappedFile);
                		
                	} 
                	
                
                sprintf(i,"%d",Test);	
                addMsg1(i, pSharedBuffer2);
                
                return 0;
                

                OnTimer()
                {
                WriteToShared()
                }

                }

                Can anyone guide,whats exactly going wrong. Thanks Sharan

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                manju 3 wrote:

                  pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE);
                

                This doesn't really make a lot of sense since you are just copying the pointer, not the contents! First, you overwrite the only pointer to a memory block on the heap that you allocated with malloc, so you create a memory leak, right there. Second, while the pointer is global and will survive the call to WriteToShared, the HANDLE you get from MapViewOfFile() will not: at the latest, it will be invalidated when you leave the function, upon which your handleMappedFile gets destroyed. At that point, pSharedBuffer2 will point to garbage! The address stored therin is invalid, and you may not access this memory. And this, exactly, happens, when next you call memset! That is your crash.

                M 1 Reply Last reply
                0
                • C CPallini

                  What is i (i.e. how is it declared/defined?)?

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  M Offline
                  M Offline
                  manju 3
                  wrote on last edited by
                  #8

                  CPallini wrote:

                  What is i

                  "i" is a flag may be 0 or 1. i am making it 0 initially,and later making it 1 and writing to shared memory

                  C 1 Reply Last reply
                  0
                  • S Stefan_Lang

                    manju 3 wrote:

                      pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE);
                    

                    This doesn't really make a lot of sense since you are just copying the pointer, not the contents! First, you overwrite the only pointer to a memory block on the heap that you allocated with malloc, so you create a memory leak, right there. Second, while the pointer is global and will survive the call to WriteToShared, the HANDLE you get from MapViewOfFile() will not: at the latest, it will be invalidated when you leave the function, upon which your handleMappedFile gets destroyed. At that point, pSharedBuffer2 will point to garbage! The address stored therin is invalid, and you may not access this memory. And this, exactly, happens, when next you call memset! That is your crash.

                    M Offline
                    M Offline
                    manju 3
                    wrote on last edited by
                    #9

                    Hi, thanks for your reply, so how can i avoid the memory crash now. thanks sharan

                    S 1 Reply Last reply
                    0
                    • M manju 3

                      CPallini wrote:

                      What is i

                      "i" is a flag may be 0 or 1. i am making it 0 initially,and later making it 1 and writing to shared memory

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #10

                      Then

                      sprintf(i,"%d",Test);

                      is evil (sprintf assumes i is an array of characters).

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      1 Reply Last reply
                      0
                      • M manju 3

                        Hi, thanks for your reply, so how can i avoid the memory crash now. thanks sharan

                        S Offline
                        S Offline
                        Stefan_Lang
                        wrote on last edited by
                        #11

                        Easy: don't call memset! Two possibilities: 1. if you don't use pShardedBuffer2 outside this function, then (a) declare it inside the function as a local parameter and (b) don't malloc, and don't memset (in this case I really don't know what you do that for) 2. if you do use it elsewhere, then you must copy the data, not the pointer! You should be aware though, that changes to that copy won't affect the original data, you can use that copy only to read from! Anyway, if you copy data to your buffer, memset is not required - the value you set your buffer to gets overwritten anyway! Note that I have no idea how to go about copying the data, as MapViewOfFile returns a HANDLE, not a string, and does not provide any information about the structure or size of that data. Accessing it like a string sounds like calling for trouble, especially if there are 0 bytes within. I'm not familiar with this function or the data structure it delivers, but I suggest you read up on it and how to use it properly. What you need to do may also depend on how you use the data later, such as in your call to addMsg1(). What does it do?

                        1 Reply Last reply
                        0
                        • M manju 3

                          Hi, Thanks for your reply.Even i tried with

                          memset(pSharedBuffer2, 0, max_msg);

                          but getting the same "access violation for writing" Thanks Sharan

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          You arent checking the return form malloc for a valid allocation.

                          ============================== Nothing to say.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups