Memory allocation/free problem 
Author Message
 Memory allocation/free problem

In my program I have a function that will call itself recursively. The
function (goofBall) have two char * that I allocate memory for using
malloc(). After calling itself a couple of times, goofBall terminates
with a couple of warnings:

lsstripped in free(): warning: page is already free
lsstripped in free(): warning: chunk is already free
lsstripped in free(): warning: page is already free
lsstripped in free(): warning: chunk is already free

To my understanding, this warning should imply that I try to free()
memory that's already freed, however, I'm not explicitly freeing
anything. Is something happening behind my back?

I would deeply appreciate any help on this matter.

Regards
Mathias Samuelson

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define FILENAME_MAX_LENGTH 255

void printInode(char[]);
char *goofBall(char[]);

int main(int argc, char *argv[]) {
     goofBall(argv[1]);
     return 0;

Quote:
}

DIR *dir_ent;

char *goofBall(char *dir) {
     struct stat inode;
     struct dirent *dp;
     char *path, *from;
     char *dirbuf = NULL;

     /* Memory allocations */
     from = (char *)malloc(FILENAME_MAX_LENGTH * sizeof(char) + 1);
     path = (char *)malloc(FILENAME_MAX_LENGTH * sizeof(char) + 1);

     from = getcwd(dirbuf, FILENAME_MAX_LENGTH * sizeof(char));

     /* To avoid having to use absolute paths in later function calls,
      * we'll chdir and just use relative d_name... */
     if ((dir_ent = opendir(dir)) == NULL) {
         perror("opendir error");
         printf("in dir: %s\n", dir);
         return NULL;
     }
     if (chdir(dir) == -1) {
         perror("chdir error");
         exit(-1);
     }

     /* Now print information for all local files */
     while ((dp = readdir(dir_ent)) != NULL) {
         if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
             continue;   // skip current dir and parent
         printInode(dp->d_name);
     }
     rewinddir(dir_ent);

     /* Now print information for files in all subdirs */
     while ((dp = readdir(dir_ent)) != NULL) {

         if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") ==
0) {
             continue;   // skip current dir and parent
         }

         // Checking each inode, if it's a dir, recurse
         sprintf(path, "%s/%s",
             getcwd(dirbuf, FILENAME_MAX_LENGTH * sizeof(char)),
dp->d_name);

         /* Check for length of path, to avoid writung outside of malloced
          * memory */

         if (strlen(path) > FILENAME_MAX_LENGTH) {
             printf("Filename/path is too long, exiting\n");
             exit(-1);
         }

         if (stat(dp->d_name, &inode) == -1) {
             perror("GoofBall stat error");
             printf("    in dir: %s, file: %s\n", dir, dp->d_name);
             return NULL;
         }
         if ((inode.st_mode & S_IFMT) == S_IFDIR) {
             // This inode is a directory
             goofBall(path);
         }
     }
     // Change back to the cwd that was used before calling the function
     if (chdir(from) == -1) {
         perror("chdir error");
         exit(-1);
     }
     closedir(dir_ent);

     return dir;

Quote:
}

void printInode(char *in) {
     /* For the intended purpose, demonstrating the malloc/free problem I'm
      * having it's enough for this function to just print its argument. */

     printf("%s\n", in);

Quote:
}



Mon, 04 Apr 2005 15:41:07 GMT  
 Memory allocation/free problem

Quote:

> In my program I have a function that will call itself recursively. The
> function (goofBall) have two char * that I allocate memory for using
> malloc(). After calling itself a couple of times, goofBall terminates
> with a couple of warnings:

> lsstripped in free(): warning: page is already free
> lsstripped in free(): warning: chunk is already free
> lsstripped in free(): warning: page is already free
> lsstripped in free(): warning: chunk is already free

> To my understanding, this warning should imply that I try to free()
> memory that's already freed, however, I'm not explicitly freeing
> anything. Is something happening behind my back?

> #include <sys/types.h>
> #include <sys/stat.h>
> #include <dirent.h>

The above are all non-standard includes.  We can't understand
things that are not defined by standard C.  It smells as if you
need a group that deals with your system.

Quote:
> #include <stdio.h>
> #include <unistd.h>

So is this.

Quote:
> #include <stdlib.h>
> #include <string.h>

> #define FILENAME_MAX_LENGTH 255

> void printInode(char[]);
> char *goofBall(char[]);

> int main(int argc, char *argv[]) {
>      goofBall(argv[1]);
>      return 0;

> }

> DIR *dir_ent;

We have no idea what a DIR is.

Quote:

> char *goofBall(char *dir) {
>      struct stat inode;
>      struct dirent *dp;

What are these types?

Quote:
>      char *path, *from;
>      char *dirbuf = NULL;

What is this type, apart from a pointer?

Quote:

>      /* Memory allocations */
>      from = (char *)malloc(FILENAME_MAX_LENGTH * sizeof(char) + 1);
>      path = (char *)malloc(FILENAME_MAX_LENGTH * sizeof(char) + 1);

Don't cast malloc return values.  sizeof(char) is 1 by
definition.  

Quote:

>      from = getcwd(dirbuf, FILENAME_MAX_LENGTH * sizeof(char));

What is getcwd?  sizeof(char) is still 1.

Quote:

>      /* To avoid having to use absolute paths in later function calls,
>       * we'll chdir and just use relative d_name... */
>      if ((dir_ent = opendir(dir)) == NULL) {

opendir is not defined.

Quote:
>          perror("opendir error");
>          printf("in dir: %s\n", dir);
>          return NULL;
>      }
>      if (chdir(dir) == -1) {

chdir is not defined.

Quote:
>          perror("chdir error");
>          exit(-1);

exit(-1) is not correct.  The only arguments possible are 0,
EXIT_SUCCESS, and EXIT_FAILURE.

Quote:
>      }

>      /* Now print information for all local files */
>      while ((dp = readdir(dir_ent)) != NULL) {
>          if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
>              continue;   // skip current dir and parent
>          printInode(dp->d_name);
>      }
>      rewinddir(dir_ent);

Sigh.  You get the idea, I hope.

- Show quoted text -

Quote:

>      /* Now print information for files in all subdirs */
>      while ((dp = readdir(dir_ent)) != NULL) {

>          if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") ==
> 0) {
>              continue;   // skip current dir and parent
>          }

>          // Checking each inode, if it's a dir, recurse
>          sprintf(path, "%s/%s",
>              getcwd(dirbuf, FILENAME_MAX_LENGTH * sizeof(char)),
> dp->d_name);

>          /* Check for length of path, to avoid writung outside of malloced
>           * memory */

>          if (strlen(path) > FILENAME_MAX_LENGTH) {
>              printf("Filename/path is too long, exiting\n");
>              exit(-1);
>          }

>          if (stat(dp->d_name, &inode) == -1) {
>              perror("GoofBall stat error");
>              printf("    in dir: %s, file: %s\n", dir, dp->d_name);
>              return NULL;
>          }
>          if ((inode.st_mode & S_IFMT) == S_IFDIR) {
>              // This inode is a directory
>              goofBall(path);
>          }
>      }
>      // Change back to the cwd that was used before calling the function
>      if (chdir(from) == -1) {
>          perror("chdir error");
>          exit(-1);
>      }
>      closedir(dir_ent);

>      return dir;
> }

> void printInode(char *in) {
>      /* For the intended purpose, demonstrating the malloc/free problem I'm
>       * having it's enough for this function to just print its argument. */

>      printf("%s\n", in);
> }

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!


Mon, 04 Apr 2005 16:06:53 GMT  
 Memory allocation/free problem

Quote:

>> DIR *dir_ent;
> We have no idea what a DIR is.

>> char *goofBall(char *dir) {
>>      struct stat inode;
>>      struct dirent *dp;
> What are these types?
>>      char *path, *from;
>>      char *dirbuf = NULL;
> What is this type, apart from a pointer?

I guess it's a pointer to a single byte, as defined in the ISO C90 and
C99 standards for the C programming language, but then, ICBW.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"The question of copying music from the Internet is like a two-barreled sword."
   - Finnish rap artist Ezkimo



Mon, 04 Apr 2005 16:13:52 GMT  
 Memory allocation/free problem

Quote:

> To my understanding, this warning should imply that I try to free()
> memory that's already freed, however, I'm not explicitly freeing
> anything. Is something happening behind my back?

Not freeing stuff you malloc is a bad idea in itself.

It could be that one of the utiltity functions you use might be calling
free(). Perhaps check that you are not calling closedir() on the same DIR*
twice?

The people at comp.unix.programmer might be able to help better.

Bill, is back.

--
 Hey there, write a robot to play Gomoku -
       http://www.bacchae.co.uk/tourk/gomoku.html
                                (I still have some 8" disks for sale)



Mon, 04 Apr 2005 16:28:05 GMT  
 Memory allocation/free problem

Quote:


>>To my understanding, this warning should imply that I try to free()
>>memory that's already freed, however, I'm not explicitly freeing
>>anything. Is something happening behind my back?

> Not freeing stuff you malloc is a bad idea in itself.

I hear you :)

Quote:

> It could be that one of the utiltity functions you use might be calling
> free(). Perhaps check that you are not calling closedir() on the same DIR*
> twice?

> The people at comp.unix.programmer might be able to help better.

Thank you, I'll try that.

Quote:
> Bill, is back.

Regards


Mon, 04 Apr 2005 23:53:18 GMT  
 Memory allocation/free problem
also check if malloc succeeded


Tue, 05 Apr 2005 04:52:44 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. dynamic memory allocation performance problems with malloc/free

2. Tree memory allocation, pool memory allocation

3. Please, help with checking memory allocation free soft

4. memory allocation using malloc and free

5. Memory Allocation and Freeing using New and Delete Operators

6. Memory Allocation and DeAllocation (new and free) !?

7. Memory allocation and free

8. Problems freeing memory using "free"

9. CString problem memory allocation problem

10. Memory Allocation Problem

11. memory allocation performance problem

12. memory allocation problem

 

 
Powered by phpBB® Forum Software