ARVIND'S NOTES ON MEMORY > sorry bout that. freed the memory before using it :p > > char *get_msg_filename(int x) > { > struct dirent **msgarray; > int msgarraynum; > msgarraynum = scandir(".", &msgarray, msg_filter, versionsort); > char *fn; > x = x-1; > fn = msgarray[x]->d_name; Fine. fn is a pointer to the d_name of msgarray[x]. > free(msgarray[x]); Ouch! You just freed the memory pointed to by fn (maybe not, depending on the implementation. But ...) A better idea would be to copy the string msgarray[x]->d_name to a new string, fn, using strdup and then, freeing the whole msgarray. > free(msgarray); Now, for msgarray. Here's how it's implemented: [Look at it in a fixed-width font as in a terminal as it's ASCII art, otherwise it'll appear skewed.] --------------------------------- | | | | | array --------------------------------- | | | | | | | | v v v v ---- ---- ---- ---- | | | | | | | | | | | | | | | | ---- ---- ---- ---- array[0] array[1] ... And, each array element is a structure according to readdir(2). Now, the two lines you have in get_msg_filename are: > free(msgarray[filenum]); > free(msgarray); So, what does the first line do? It frees one of those little boxes (array[filenum]). Then, the second lines frees the array (ie, the top set of boxes). So, what are you left with? Something like this: | | | | | | v v v ---- ---- ---- | | | | | | | | | | | | ---- ---- ---- array[0] array[1] ... Do you see a memory leak there? What do you do to get rid of it? Also, you see the missing box there (third box), right. It used to have the filename, fn. You've freed it (msgarray[filenum]). You destroyed memory pointed to by a pointer, and you continue to use the memory, which is very dangerous. So, make a copy of the filename string. Arvind -- Q: How do you play religious roulette? A: You stand around in a circle and blaspheme and see who gets struck by lightning first. Next message: > I think I fixed it. > > http://geocities.com/latestringtones2003/grim-0.0.7a.txt Hmm, no. It isn't fixed. I'm trying hard not to directly tell you what to do. So, let me show you how a namelist like the "struct dirent ***namelist" is allocated memory. By the way, you know that passing an int to a function, like so: int test (int x) { ... won't let the "x" be changed by the function, don't you? And, passing a pointer to the int, like so: int test (int *x) { ... will allow the "x" to be changed. Likewise, scandir takes a "struct dirent ***namelist". Notice the three asterisks. The third one (one on the left) is just to allow a **namelist to be changed by the function. So, how do you allocate memory for **namelist? You cannot simply do a: namelist = malloc (somesize). It's a two step process: namelist = allocate enough memory for 4 objects of type pointer to struct dirent. OR namelist = malloc (5 * sizeof (struct dirent *)); OR namelist = new dirent *[5] Now, namelist is: ----- namelist[0] | | | | | | ----- namelist[1] | | | | | | ----- namelist[2] | | | | | | ----- namelist[3] | | | | | | ----- namelist[4] | | | | | | ----- Each of the blocks above is a pointer to a struct dirent, and not a struct dirent object. So, it cannot have a d_name or any other thing in it. Again, namelist[i] is not a dirent object. It's a *pointer* to a dirent object. Each of the blocks above will be associated with a struct dirent object. This is done in a for loop. for (int i = 0; i < 5; i++) { namelist[i] = new dirent; OR namelist[i] = malloc (sizeof (struct dirent)); } Now, namelist is: ----- namelist[0] | | ------------ | |---------->| | | | ------------ ----- namelist[1] | | ------------ | |---------->| | | | ------------ ----- namelist[2] | | ------------ | |---------->| | | | ------------ ----- namelist[3] | | ------------ | |---------->| | | | ------------ ----- namelist[4] | | ------------ | |---------->| | | | ------------ ----- So, how do you free the memory allocated? If you do a: free(msgarray); like you've done, then, it's like doing a: free (namelist); What happens? What do you have to free first? Arvind -- If there is no God, who pops up the next Kleenex? -- Art Hoppe