Thread: Memory leaking all over the place

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    8

    Unhappy Memory leaking all over the place

    Hello. As part of my program, I have implemented a stack (LIFO) structure, which stores void pointers as its data (seeing as it's intented to store pointers to any type of variable). Here is it's deceleration:
    Code:
    typedef struct
    {
        void** data;
        unsigned int size;
    } stack;
    Another part of my program stores these stacks which will contain 'symbols' (a struct) inside another one of these stacks (a super-stack if you will).

    A function called 'enter_scope' creates one of these stacks and pushes it into the super-stack:
    Code:
    int enter_scope(stack* sym_stack)
    {
        stk_push(sym_stack, stk_create());
    
        if(errno != 0) return 0;
        return 1;
    }
    Another function called 'exit_scope' means to delete all the symbol structs held in top stack of the super stack, then delete that top stack. However, I cannot seem to implement this without causing memory leaks. Here is what I have so far:
    Code:
    int exit_scope(stack* sym_stack)
    {
        stack** top_scope = stk_top(sym_stack);
    
        while((*top_scope)->size)
        {
            symbol* top_symbol = stk_top(*top_scope);
    
            free(top_symbol->data);
            free(top_symbol);
    
            stk_pop(*top_scope);
        }
    
        free(*top_scope);
    
        stk_pop(sym_stack);
    
        if(errno != 0) return 0;
        return 1;
    }
    If you want to know the output of Valgrind when I run this as my main function:
    Code:
    int main()
    {
        stack* symstk = stk_create();
    
        enter_scope(symstk);
    
        exit_scope(symstk);
    
        symstk_destroy(symstk);
    
        return 1;
    }
    You can find it here: Valgrind output - Pastebin.com.

    You may also want to know the contents of stk_top, stk_push and stk_pop.

    stk_top:
    Code:
    void* stk_top(stack* stk)
    {
        if(stk->size == 0)
        {
            errno = E_STKTOPEMPTY;
            return NULL;
        }
    
        return stk->data + (stk->size - 1);
    }
    stk_push:
    Code:
    int stk_push(stack* stk, void* new_item)
    {
        if((stk->data = realloc(stk->data, sizeof(void*) * ++ stk->size)) == NULL)
        {
            errno = E_STKREALLOC;
            return 0;
        }
    
        memmove(stk->data + (stk->size - 1), new_item, sizeof(void*));
    
        return 1;
    }
    stk_pop:
    Code:
    int stk_pop(stack* stk)
    {
        if(stk->size == 0)
        {
            errno = E_STKPOPEMPTY;
            return 0;
        }
    
        if(((stk->data = realloc(stk->data, sizeof(void*) * -- stk->size)) == NULL) && (stk->size != 0))
        {
            errno = E_STKREALLOC;
            return 0;
        }
    
        return 1;
    }
    Any and all help will be much appreciated.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What is stk_create()?

    Your realloc usage is incorrect - in case of failure you get a memory leak.

    Code:
    void * data = NULL;
    ...
    
    void* temp = realloc(data, newsize);
    if(temp == NULL)
    {
        //continue to use data pointer with old size
    }
    else
    {
       data = temp;
       // use new data pointer with new size
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    8
    stk_create:
    Code:
    stack* stk_create()
    {
        stack* new_stack = malloc(sizeof(stack));
    
        if(new_stack == NULL)
        {
            errno = E_STKALLOC;
            return NULL;
        }
    
        if((new_stack->data = malloc(sizeof(void*))) == NULL)
        {
            errno = E_STKALLOC;
            return NULL;
        }
    
        new_stack->size = 0;
    
        return new_stack;
    }
    If realloc had failed and returned NULL, then errno would have been set to E_STKREALLOC. I just set my main function to return errno after running it I found out that it wasn't set, so it would appear that realloc didn't fail anyway.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I'm not really grasping your logic here, but what I see

    Your stk_create() allocates new_stack->data and sets size to 0

    When size is 0 your exit_scope function does not frees data member of something poped from the stack...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Based only on symmetry I would suspect looking at the
    Code:
    int enter_scope(stack* sym_stack)
    {
        stk_push(sym_stack, stk_create());
     
        if(errno != 0) return 0;
        return 1;
    }
    That exit scope should be like
    Code:
    int exit_scope(stack* sym_stack)
    {
        void * data = stk_pop(sym_stack); /* why your pop function
        does not returns removed element I do not know
     - it is not like regular stack will behave */
       stk_destruct (data);
       return 1;
    }
    Last edited by vart; 07-26-2013 at 11:06 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is dlopen leaking memory?
    By raczzoli in forum C Programming
    Replies: 1
    Last Post: 09-26-2012, 03:03 PM
  2. gmp/pthread program leaking memory. Why?
    By sadakatsu in forum C Programming
    Replies: 5
    Last Post: 03-01-2012, 08:34 PM
  3. Is my SDL program leaking memory?
    By LyTning94 in forum C++ Programming
    Replies: 6
    Last Post: 07-19-2011, 01:18 PM
  4. Leaking memory when creating threads
    By Elkvis in forum Windows Programming
    Replies: 3
    Last Post: 08-18-2009, 02:27 PM
  5. traping leaking memory
    By null in forum C Programming
    Replies: 5
    Last Post: 10-01-2001, 12:02 PM