Using an electric fence in a C ++ program

I have been experimenting with Electric Fence recently, and I can't figure out how to use it with C ++ code.

Here is an example:

// test.cpp
#include <cstdlib>                                                                                                                                         

using namespace std;                                                                                                                                       

int main()                                                                                                                                                 
{                                                                                                                                                                                                                                                                                                     
        int *a = new int(10);                                                                                                                              
        delete a;                                                                                                                              
}  

I compiled it with

g++ ./test.cpp -o test -lefence -L/home/bor/efence_x86_64/lib -lpthread

And I do not see the Electric Fence banner at the beginning and cannot find the EF characters in the executable (using the nm command).

But if I change a program like this:

// test.cpp
#include <cstdlib>                                                                                                                                         

using namespace std;                                                                                                                                       

int main()                                                                                                                                                 
{                                                                                                                                                          
        char *p = (char*)malloc(20);                                                                                                                       
        free(p);                                                                                                                                           
        int *a = new int(10);                                                                                                                              
        delete a;
}

all is well - appears EF. I know this solves the problem, I know :). I just want to understand why this did not work in the first place, because it new()should call malloc(), but it delete()calls free(), no?

, , - boost . malloc() free() . EF, EF , , EF . EF . ? , EF , libs ? - EF .

+5
4

, new, - RT.

new malloc ( , Windows, malloc), , , . - , , new delete, malloc free .

+3

slackware   http://slackbuilds.org/repository/13.1/libraries/electric-fence/

In order to debug a program it needs to be linked with Electric Fence's
library or dynamic linking needs to be used; README.Debian explains that
in detail.


If you're using c++, and you and want to statically link your c++
programs, you shouldn't use g++ to link libefence.a, but rather:
gcc -o myprog myprog.o -lstdc++ -lg++ -lefence
(if you use g++, the order is different, and efence malloc doesn't
get used)

man- libefence, , , lebefence

+1

, "" ++ armv5:

. :

LD_PRELOAD=libefence.so ./your-buggy-program

( , )

+1

"mallocs", "-libefence" "ldd". , "mallocs" "new", "-libefence" build.

0

All Articles