How to Force Abort on "Glibc Detected *** Free(): Invalid Pointer"

problem with containers: *** glibc detected *** free(): invalid pointer: 0x41e0ce94 ***

Consider using a std::string to hold the string value instead of a raw char pointer. Then you won't have to worry about managing the string data in your assignment, copy, and destruction methods. Most likely your problem lies there.

Edit: There's no issue with the newer class you posted, and no problem with the first version if you're only using the char * to point to string constants. The problem lies elsewhere in the program or with the way you're using the class. You'll have to spend more time digging in the debugger and/or valgrind to track down the problem. I would figure out what is pointed to at the specified address and try determine why it's being freed twice.

*** glibc detected *** outfile: free(): invalid pointer: ***


    char *sub_dir = (char *)malloc(sizeof(char) * MAX_SIZE);
strcpy( sub_dir, input_dir );
strcat( sub_dir, dp->d_name);
strcat( sub_dir, "/");
open_read(sub_dir, g_node_insert(parent,-1,g_node_new(dp->d_name)) );

Okay, so what you pass to g_node_new is not what malloc returned, so you certainly can't pass that to free.

gboolean destroyer(GNode* node, gpointer data)
{
printf("\nfrom destroyer: ");
printf("%pnode\t data %p\n",node,node->data);
free(node->data);
return SUCCESS;
}

But you do!

I believe you intended:

    open_read(sub_dir, g_node_insert(parent,-1,g_node_new(sub_dir)) );

Getting a `free()` error when deallocating with `delete` in the backtrace

Raw memory allocation/deallocation mechanism behind new[]/delete[] is typically the same as the one used by malloc/free. Standard library implementation of raw memory allocation/deallocation functions operator new[]/operator delete[] can actually directly call malloc and free. For this reason, it is completely unsurprising that the error is reported by free even if you are using delete [].

The error that you are getting indicates that the integrity of heap is violated. The heap is broken. The source of the problem can be in this function (double free?) or in some completely different place (double free or memory overrun?). There's no way to say what is happening from the code you posted.

Figure out which specific call to delete [] reports the problem and see if some other code overrides that memory block. Or just use some external tool like valgrind to catch the offender.



Related Topics



Leave a reply



Submit