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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Free fails with Access violation reading location

Free fails with Access violation reading location

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancequestion
10 Posts 4 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.
  • T Offline
    T Offline
    tejaswini_g
    wrote on last edited by
    #1

    Can anyone help what is wrong in the following code snippet.

    free (Filelist[i]);

    fails with access violation error. Thanks in advance. int _tmain(int argc, TCHAR *argv[]) { int i =0; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return; } } FindFilesInDirectory(szDirpath, Filelist, &iFileCountInFolder); if (Filelist != NULL) { for ( i = 0; i < iFileCountInFolder ; i++)

    //following line failing with access voilation
    free (Filelist[i]);

    free (Filelist); } return 0; }

    Richard Andrew x64R L L 4 Replies Last reply
    0
    • T tejaswini_g

      Can anyone help what is wrong in the following code snippet.

      free (Filelist[i]);

      fails with access violation error. Thanks in advance. int _tmain(int argc, TCHAR *argv[]) { int i =0; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return; } } FindFilesInDirectory(szDirpath, Filelist, &iFileCountInFolder); if (Filelist != NULL) { for ( i = 0; i < iFileCountInFolder ; i++)

      //following line failing with access voilation
      free (Filelist[i]);

      free (Filelist); } return 0; }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      First, put your code between <pre></pre> tags so that it's readable. Second, the code you cite as causing the error is not even in the snippet you show. :confused:

      The difficult we do right away... ...the impossible takes slightly longer.

      T 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        First, put your code between <pre></pre> tags so that it's readable. Second, the code you cite as causing the error is not even in the snippet you show. :confused:

        The difficult we do right away... ...the impossible takes slightly longer.

        T Offline
        T Offline
        tejaswini_g
        wrote on last edited by
        #3

        re posted.

        1 Reply Last reply
        0
        • T tejaswini_g

          Can anyone help what is wrong in the following code snippet.

          free (Filelist[i]);

          fails with access violation error. Thanks in advance. int _tmain(int argc, TCHAR *argv[]) { int i =0; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return; } } FindFilesInDirectory(szDirpath, Filelist, &iFileCountInFolder); if (Filelist != NULL) { for ( i = 0; i < iFileCountInFolder ; i++)

          //following line failing with access voilation
          free (Filelist[i]);

          free (Filelist); } return 0; }

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          The mallocs and frees are fine in concept the issue is deeper I suspect this is the problem

          Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1);

          iFileSize is initially set to zero and then passed in as a parameter to FileCountInDir which I guess is supposed to fill it in. But I find it really strange because that function is counting the directory entries from it's function name. Really the string space allocation should be the length of each individual directory entry name so I am not sure it should be a single value. For example a file "fred.bmp" should allocate 9 bytes (8+asciiz 0) while "afred.bmp" should allocate 10 bytes (9+asciiz 0) I fail to see how you are allocating them all as iFileSize unless that is the largest filename in the directory. And that leads to the answer for your fail that iFilesize must be coming back from FileCountInDir as -1 and that means you are allocating -1+1 = 0 bytes of memory which will fail in malloc returning NULL. Hence Filelist[i] would indeed be carrying NULL You can put protection to stop the error by doing this

          if (FileList[i] != 0) free (Filelist[i]);

          But as discussed I think you have serious issues with iFileSize

          L 2 Replies Last reply
          0
          • T tejaswini_g

            Can anyone help what is wrong in the following code snippet.

            free (Filelist[i]);

            fails with access violation error. Thanks in advance. int _tmain(int argc, TCHAR *argv[]) { int i =0; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return; } } FindFilesInDirectory(szDirpath, Filelist, &iFileCountInFolder); if (Filelist != NULL) { for ( i = 0; i < iFileCountInFolder ; i++)

            //following line failing with access voilation
            free (Filelist[i]);

            free (Filelist); } return 0; }

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

            If you are posting a lump of code a compile ready snippet helps everybody.

            Peter Wasser "The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts." - Bertrand Russell

            T 1 Reply Last reply
            0
            • L leon de boer

              The mallocs and frees are fine in concept the issue is deeper I suspect this is the problem

              Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1);

              iFileSize is initially set to zero and then passed in as a parameter to FileCountInDir which I guess is supposed to fill it in. But I find it really strange because that function is counting the directory entries from it's function name. Really the string space allocation should be the length of each individual directory entry name so I am not sure it should be a single value. For example a file "fred.bmp" should allocate 9 bytes (8+asciiz 0) while "afred.bmp" should allocate 10 bytes (9+asciiz 0) I fail to see how you are allocating them all as iFileSize unless that is the largest filename in the directory. And that leads to the answer for your fail that iFilesize must be coming back from FileCountInDir as -1 and that means you are allocating -1+1 = 0 bytes of memory which will fail in malloc returning NULL. Hence Filelist[i] would indeed be carrying NULL You can put protection to stop the error by doing this

              if (FileList[i] != 0) free (Filelist[i]);

              But as discussed I think you have serious issues with iFileSize

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

              I think you are on to it as the mallocs and frees seem OK. Unfortunately not enough information provided.

              Peter Wasser "The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts." - Bertrand Russell

              1 Reply Last reply
              0
              • L Lost User

                If you are posting a lump of code a compile ready snippet helps everybody.

                Peter Wasser "The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts." - Bertrand Russell

                T Offline
                T Offline
                tejaswini_g
                wrote on last edited by
                #7

                I am posting compiled code. As stated above Freelist[i] fails. Highlighted the statement causing error. #include <> // remove extra angle bracket for compilation #include <> #include <> #include <> void FreeMemory(char** myArray, int row); void FileCountInDir(const TCHAR* dirpath, int * filescount, int* filesize); void FindFilesInDirectory(const TCHAR* dirpath, char** filelist); int _tmain(int argc, TCHAR *argv[]) { int i =0; TCHAR szDirpath[MAX_PATH]; //TCHAR Filelist[100][MAX_PATH]; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return 0; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return 0; } } FindFilesInDirectory(szDirpath, Filelist); for(i =0;i

                1 Reply Last reply
                0
                • L leon de boer

                  The mallocs and frees are fine in concept the issue is deeper I suspect this is the problem

                  Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1);

                  iFileSize is initially set to zero and then passed in as a parameter to FileCountInDir which I guess is supposed to fill it in. But I find it really strange because that function is counting the directory entries from it's function name. Really the string space allocation should be the length of each individual directory entry name so I am not sure it should be a single value. For example a file "fred.bmp" should allocate 9 bytes (8+asciiz 0) while "afred.bmp" should allocate 10 bytes (9+asciiz 0) I fail to see how you are allocating them all as iFileSize unless that is the largest filename in the directory. And that leads to the answer for your fail that iFilesize must be coming back from FileCountInDir as -1 and that means you are allocating -1+1 = 0 bytes of memory which will fail in malloc returning NULL. Hence Filelist[i] would indeed be carrying NULL You can put protection to stop the error by doing this

                  if (FileList[i] != 0) free (Filelist[i]);

                  But as discussed I think you have serious issues with iFileSize

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

                  malloc()-ing 0 bytes doesn't return NULL when I test. This appears to be implementation dependent.

                  Peter Wasser "The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts." - Bertrand Russell

                  1 Reply Last reply
                  0
                  • T tejaswini_g

                    Can anyone help what is wrong in the following code snippet.

                    free (Filelist[i]);

                    fails with access violation error. Thanks in advance. int _tmain(int argc, TCHAR *argv[]) { int i =0; char ** Filelist; int iFileCountInFolder=0; int iFileSize = 0; _tcscpy(szDirpath,_T("C:\\BMPread_W\\TransitionEffects\\Images")); FileCountInDir(szDirpath,&iFileCountInFolder,&iFileSize); Filelist = (char**)malloc(iFileCountInFolder * sizeof(char *)); if(Filelist == NULL) { fprintf(stderr, "out of memory\n"); return; } for(i = 0; i < iFileCountInFolder; i++) { Filelist[i] = (char*)malloc((iFileSize * sizeof(char))+1); if(Filelist[i] == NULL) { fprintf(stderr, "out of memory\n"); return; } } FindFilesInDirectory(szDirpath, Filelist, &iFileCountInFolder); if (Filelist != NULL) { for ( i = 0; i < iFileCountInFolder ; i++)

                    //following line failing with access voilation
                    free (Filelist[i]);

                    free (Filelist); } return 0; }

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

                    Firstly you should employ assert() to help you. This is a classic situation where many asserts can assist. By the look of your code iFileCountInFolder can change during execution. If it does get bigger on the free than on the malloc then "poof".

                    Peter Wasser "The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts." - Bertrand Russell

                    T 1 Reply Last reply
                    0
                    • L Lost User

                      Firstly you should employ assert() to help you. This is a classic situation where many asserts can assist. By the look of your code iFileCountInFolder can change during execution. If it does get bigger on the free than on the malloc then "poof".

                      Peter Wasser "The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts." - Bertrand Russell

                      T Offline
                      T Offline
                      tejaswini_g
                      wrote on last edited by
                      #10

                      iFileCountInFolder is always fixed, since the files in a folder are fixed , means no files are added at run time. I did not understand why the Free statement is failing.

                      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