Thread: Debug Assertion error when freeing memory

  1. #1
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    Debug Assertion error when freeing memory

    This is invariably to do with the way I am freeing memory here, can anyone tell me exactly how/why?

    This could be anything, but I am hopped up on goof ballz trying to keep the flu away at the moment.. Not thinking straight!

    Using MS VC++ V6.0
    Windows XP

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct 
    {
    	void *foo;
    } MYSTRUCT1;
    
    typedef struct
    {
    	int data;
    
    } MYSTRUCT2;
    
    int main(void)
    {
    	MYSTRUCT1 *struct1;
    	MYSTRUCT2 *struct2;
    
    	struct1 = malloc (sizeof(MYSTRUCT2));
    	struct2 = malloc (sizeof(MYSTRUCT2));
    
    	struct1->foo = malloc (sizeof(MYSTRUCT2));
    
    	printf("Enter a number: ");
    	scanf("%d", &struct2->data);
    
    	struct1->foo = struct2;
    
    	
    	free(struct1->foo);
    	free(struct2);
    	free(struct1);
    
    	return 0;
    }
    This is the error (in a popup window)

    Debug Assertion Failed!

    Program c:\programming\foo\Debug\foo.exe
    File: dbgheap.c
    Line: 1017
    Expression: _BLOCK_TYPE_IS_INVALID(pHead->nBlockUse)

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Foniks,

    I would expect free() to cause a nasty error if you attempted to deallocate a memory address that was never allocated. Let's take a code walk, shall we?

    struct1 = malloc (sizeof(MYSTRUCT2));

    Assuming malloc() doesn't fail, struct1 now holds an address on the heap.

    struct2 = malloc (sizeof(MYSTRUCT2));

    Assuming malloc() doesn't fail, struct2 now holds a different address on the heap.

    struct1->foo = struct2;

    struct->foo now holds the address of struct2. They both point to the same location on the heap.

    free(struct1->foo);
    free(struct2);


    The first call to free() succeeds; however the second call will fail because you are trying to free a location of memory that is no longer allocated. This has the same effect as:

    free(struct2);
    free(struct2);


    You may also want to get into the habit of checking the return value of malloc(). If malloc() fails, it will return NULL.

    Cheers,
    Jason Deckard

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Cheers!

    Oh, and don't worry, I usually test to ensure that the call to malloc has succeeded.

    Thanks again - I appreciate the help!
    Last edited by foniks munkee; 03-01-2002 at 04:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Assertion Error
    By Artist_of_dream in forum C++ Programming
    Replies: 18
    Last Post: 11-21-2004, 04:04 PM
  2. debug assertion failed?!?
    By ichijoji in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 03:23 PM
  3. Debug Assertion Failed!
    By Ray Schmidt in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2003, 09:58 PM
  4. How to tackle a debug assertion failure?
    By juhigarg in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2001, 12:59 PM
  5. Allcoating memory and not freeing it
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 09-05-2001, 11:32 AM