Re[2]: Memory leak in g_string_sized_new()?




True about the first string.  I just put that call in there because
otherwise memprof would show 0k allocated until I allocated the test
string, then jump to 2236 bytes or so.  I just wanted to start with a
reliable baseline measuerement, so that I could be sure that the changes in
allocated bytes were due solely to the calls which I was making to 
g_string_sized_new().

Anyway, I tested your program, and it works better, but 4 bytes are still
leaked.  These were my results with your program: (I added a line to show
the allocated size of both strings), and I put a getchar between the two
calls to free the strings so that I can see both freed individually.

First getchar():  2236 bytes (temp1 already allocated)

Second getchar():  2252 bytes (both temp1 and temp2 are now allocated) 16
bytes were allocated, presumably for temp2.

Third getchar():  String allocated sizes:
temp1->allocated_len=4
temp2->allocated_len=8

fourth getchar():  2244 bytes (Just freed temp2, 8 bytes freed.  I would
have expected 16 bytes freed here)

fifth getchar(): 2240 bytes (just freed temp1, 4 bytes freed.  Since I do
not know how much was originally allocated for temp1, I don't know if this
is the correct amount or not)

So, I don't think that this works as is.

Here is the code that I used, only slightly changed from what you gave me:
#include <glib.h>
#include <stdio.h>

int main() {
         GString *temp1,*temp2;
         int size=5;

         temp1=g_string_new(NULL);
         printf("About to allocate string\n");
         getchar();
         temp2=g_string_sized_new(size);
         getchar();
         printf("temp1 size: %i alloc:%i\n", temp1->len,  temp1->allocated_len);
         printf("temp2 size: %i alloc:%i\n", temp2->len,  temp2->allocated_len);
         printf("About to free string temp2.\n");
         getchar();
         g_string_free(temp2, TRUE);
         getchar();
         printf("About to free string temp1.\n");
         g_string_free(temp1,TRUE);
         getchar();
         printf("Freed strings\n");
         return 0;
}


On Tue, 21 Jan 2003 02:32:27 -0600 Charles Schmidt <cschmidt2 emich edu> wrote:

Ronald Roth wrote:

#include <glib.h>
#include <stdio.h>

int main() {
        GString *temp;
        int size=5;

        temp=g_string_new(NULL);
        printf("About to allocate string\n");
        getchar();
        temp=g_string_sized_new(size);
        getchar();
        printf("temp size: %i alloc:%i\n", temp->len,
temp->allocated_len);
        printf("About to free string.\n");
        getchar();
        g_string_free(temp, TRUE);
        return 0;
}

The first GString you create, with g_string_new(NULL) is never free'd. 
This code shouldn't (but I havn't tested it) leak:

#include <glib.h>
#include <stdio.h>

int main() {
         GString *temp1,*temp2;
         int size=5;

         temp1=g_string_new(NULL);
         printf("About to allocate string\n");
         getchar();
         temp2=g_string_sized_new(size);
         getchar();
         printf("temp size: %i alloc:%i\n", temp2->len, 
temp2->allocated_len);
         printf("About to free string.\n");
         getchar();
         g_string_free(temp1, TRUE);
        g_string_free(temp2,TRUE);
         return 0;
}

Hope this helps











[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]