realoc failed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saee
    New Member
    • Feb 2009
    • 24

    realoc failed

    i am using realloc several times for the same pointer.
    it does allocate memory 1-2 times and then throws an error debug assertion failed.

    file: dbgheap.c
    line : 609

    expression : _CtrIsValidHeap Pointer(pUserDa ta)


    at this line in code.


    cp->class2q->enqptr = (qptr*)realloc( cp->class2q->enqptr,
    sizeof(cir_q)*( cp->class2q->count + 1));
  • saee
    New Member
    • Feb 2009
    • 24

    #2
    on abort code exits with code 03.

    Comment

    • saee
      New Member
      • Feb 2009
      • 24

      #3
      i bypassed realloc. used malloc and copying existing block to new memory manually.
      but i want to know whts wrong with realloc.

      now i am facing another assertion error with free()

      file : dbgheap.c
      line 1017
      expression :
      _BLOCK_TYPE_IS_ VALID(pHead->nBlockUse)

      in watch window i can see the pointer i want to free is a valid pointer.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Code:
        cp->class2q->enqptr = (qptr*)realloc( cp->class2q->enqptr, sizeof(cir_q)*(cp->class2q->count + 1));
        This should work fine, you should not need to cast the output of realloc unless you are using c++ in which case why aren't you using a vector?

        When you said you replaced the call to realloc with a call to malloc I hope you meant malloc and free. If not your code is now leaking memory all over the place (if it wasn't already) and you have effectively hidden whatever error was happening before.

        An error at

        expression : _CtrIsValidHeap Pointer(pUserDa ta)

        probably means you tried to use a pointer in realloc that had already been freed somewhere. However it could mean that another piece of your program is writing outside the bounds of valid data objects and overwriting and corrupting the pointers you hold which the MSC++ debug heap then detects when you try to free the memory blocks.

        Comment

        • saee
          New Member
          • Feb 2009
          • 24

          #5
          hi banfa. thank you for the answer. yes it works fine twice always. but wen it is called 3rd time it gives assertion error.

          "However it could mean that another piece of your program is writing outside the bounds of valid data objects and overwriting and corrupting the pointers you hold which the MSC++ debug heap then detects when you try to free the memory blocks."

          yes. thats why it gives me assertion error with free( ). how much is valid data object space?
          i am not dealing directly with memory other than caling realloc here.
          i do have some global and static variables.and i am allocating/realocating memory to a global variable.
          as these variales are stored by compiler they must be within the valid space.
          ryt??
          actually i don't know how to check for the valid space in memory and as they are stored by compiler itself how do i manage them?
          thanks.

          Comment

          • saee
            New Member
            • Feb 2009
            • 24

            #6
            hi this is my realloc function
            i have replaced all malloc/realloc by my_realloc.
            i am free ing memory at realloc.
            -----------------------------------------------------------------------------------------
            char* my_realloc (void * current, int size)
            {
            char * temp;

            temp = malloc(size);
            if(temp == NULL) return NULL;
            if(current == NULL) return temp;

            memcpy(temp, current, size);

            free(current);

            return temp;
            }
            -----------------------------------------------------------------------------------------

            it gives me error at line - free(current);
            that means it was written out of the bounds.
            only my_realloc is calling memory functions.

            how do i prevent it??
            have i got my_realloc right?
            :(

            Comment

            Working...