Reason for “glibc – detected” and the possible cause

Posted on by By Nikhilesh, in Miscellaneous | 0

Most common reason for glibc – detected:-

  • When we are trying to free some memory  which is logically not available for “free ()” .
  • When we are trying to free some memory which is already free.
  • When we are trying to use multiple “free()”  for the same variable.
  • When we are trying to free pointers.
  • When we are trying to free the variable from outside the scope of variable.
  • Using an uninitialized pointer.
  • Writing outside your array bounds.

To avoid or solve glibc detection follows some basic and common rule:-

  1.    Set pointer to NULL after free
  2.    Check for NULL before freeing.
  3.    Initialize pointer to NULL in the start.
  4.    Always allocate storage blocks with calloc or some other means of initializing every block to   known all-zeros.
  5.    Write test-scripts to independently exercise all major pieces of the program in isolation to the others. Run these tests all the time to catch regressions.
  6.    Use debugging-tools that add “eyecatchers” around storage-blocks to detect “off-by-one” overwrites and other similar problems. Consider leaving them in, in the production(!) code.


int main()


char* s = (char*) malloc(sizeof(char)*10);




Here we get  “** glibc detected *** free(): invalid pointer: 0x0000000000400b2c”

Solution-        s=”hello”;

Here we are assigning another address to s, to a statically allocated memory. Freeing it is not correct. Also, since we are doing this, we are basically leaking the memory you have allocated here:

.                     char* s =(char*) malloc(sizeof(char)*10);

#include <stdio.h>

#include <string.h>


int main(){

static const size_t kBufferSize = 10;

char* s = (char*)malloc(sizeof(char)*kBufferSize);

strncpy(s,”hello”, kBufferSize); // better than strcpy, you are protecting

// yourself from a buffer overflow





Best Open Source Business Intelligence Software Helical Insight is Here


A Business Intelligence Framework

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments