Electric  Fence helps you detect two common programming bugs: software that overruns the boundaries of a malloc() memory allocation, and software that touches a memory allocation that has been released by free(). Unlike other malloc() debuggers, Electric Fence will detect  read  accesses as well as writes, and it will pinpoint the exact instruction that causes an error. Create a test program which has some memory leaks for understanding how to debug memory leaks with electric fence, $ vim efence.c #include <stdio.h> #include <stdlib.h> int main() { char *ptr = (char *) malloc(1024); ptr[0] = 0; /* Now write beyond the block */ ptr[1024] = 0; exit(0); } Now, we will need to install electric-fence package on ubuntu as, $ sudo apt-get install electric-fence Now, lets compile the program fr understanding without linking to electric fence, $ gcc -o efence efence.c $ ./efence So, here when we execute the binary without linked with electric fence, it didn’t shown any error, Now, lets link electric fence, $ gcc -o efence efence.c -lefence $ ./efence Electric Fence 2.2 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com> Segmentation fault (core dumped) Here, as we see the binary after executation return “Seg fault” which identifies the issue, now to identify exact code, we will use gdb as below, $ gcc -g -o efence efence.c -lefence $ gdb efence GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1 Copyright (C) 2014 Free Software Foundation, Inc.

Command line execution

Terminalbash
sed by free(). Unlike other malloc() debuggers, Electric Fence will detect  read  accesses as well as writes, and it will pinpoint the exact instruction that causes an error. Create a test program which has some memory leaks for understanding how to debug memory leaks with electric fence,  $ vim efence.c  #include <stdio.h> #include <stdlib.h> int main() {         char *ptr = (char *) malloc(1024);         ptr[0] = 0;         /* Now write beyond the block */         ptr[1024] = 0;         exit(0); } Now, we will need to install electric-fence package on ubuntu as, $ sudo apt-get install electric-fence Now, lets compile the program fr understanding without linking to electric fence, $ gcc -o efence efence.c  $ ./efence So, here when we execute the binary without linked with electric fence, it didn’t shown any error, Now, lets link electric fence, $ gcc -o efence efence.c -lefence  $ ./efence Electric Fence 2.2 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com> Segmentation fault (core dumped) Here, as we see the binary after executation return “Seg fault” which identifies the issue, now to identify exact code, we will use gdb as below, $ gcc -g -o efence efence.c -lefence  $ gdb efence GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1 Copyright (C) 2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type “show copying” and “show warranty” for details. This GDB was configured as “i686-linux-gnu”. Type “show configuration” for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type “help”. Type “apropos word” to search for commands related to “word”… Reading symbols from efence…done.  (gdb) r  Starting program: efence [Thread debugging using libthread_db enabled] Using host libthread_db library “/lib/i386-linux-gnu/libthread_db.so.1”. Electric Fence 2.2 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com> Program received signal SIGSEGV, Segmentation fault. 0x08048596 in main () at efence.c:8 8 ptr[1024] = 0; (gdb)

Implementation details

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type “show copying” and “show warranty” for details. This GDB was configured as “i686-linux-gnu”. Type “show configuration” for configuration details.

Gotchas and common issues

  • Permission checks - verify user access rights and sudo privileges before executing system-level operations.

  • Environment configuration - double-check path variables and dependency versions to prevent runtime failures.

  • Backup safeguards - maintain configuration backups before applying system or database modifications.

Following these steps ensures clean configuration and reliable execution for using electric fence for debugging memory leaks in linux.