How to detect memory leak of C++/C program in Linux @ 071's blog :: 隨意窩 Xuite日誌
    1. 沒有新回應!
  • 201909230946How to detect memory leak of C++/C program in Linux

      Please reference follow url:

    http://wyw.dcweb.cn/leakage.htm

    source code download from follow url(nvwa-1.1.zip):

     https://sourceforge.net/projects/nvwa/files/nvwa/0.8/

    put the follow makefile, to build nvwa as library (.so)

    #
    # Makefile
    # Created on: 2019/09/19
    # Author: Antony.Lin
    #
    PRJ_PATH = $(shell pwd|sed 's/\/app\/.*/\//g'|awk '{print}')
    APP_PATH = $(PRJ_PATH)app/
    REL_LIB = $(APP_PATH)lib

    C_COMPILER = $(CROSS_COMPILE)g++
    AR = $(CROSS_COMPILE)ar
    STRIP = $(CROSS_COMPILE)strip
    CC = $(C_COMPILER)


    INCLUDE = .
    CFLAGS = -std=c++11

    LIBS:= -lpthread -lm -lstdc++

    SRC = $(shell find ./ -name '*\.cpp')
    OBJ = $(patsubst %.cpp,%.o,$(SRC))
    DEP = $(patsubst %.cpp,%.d,$(SRC))

    TARGET := libnvwa

    all: $(OBJ) $(TARGET) install

    $(TARGET): $(OBJ)
    $(CC) -shared -o $(TARGET).so $(OBJ) $(CFLAGS) $(LIBS)
    $(STRIP) -s $(TARGET).so

    install:
    cp $(TARGET).so $(REL_LIB)

    %.o: %.cpp
    $(CC) -c $(CFLAGS) $< -o $@

    clean:
    rm -rf *.o *~ $(TARGET) $(OBJS) $(DEP) ./*.o ./*.d

    -include $(DEP)
    #end of Makefile

     

    1.      使用時請在makefile加入 include / link path

    #export NUWA = WITH_NUWA

    ifeq ($(NUWA), WITH_NUWA)

    INCLUDE += -I $(APP_PATH)nvwa

    CFLAGS += -D_DEBUG

    LDFLAGS +=-lnvwa

    Endif

     

    2.      請於要偵測的程式檔(.c/.cpp) ,檔頭加入

    #ifdef _DEBUG

    #define _DEBUG_NEW_EMULATE_MALLOC 1

    #include "debug_new.h"

    #endif

     

    3.      請用g++ compile , 如果原本是c程式, 並解決相關相容性問題(cc++) , 如指標轉型等的問題.

    py module|日誌首頁|C# : zip extrac...上一篇py module下一篇C# : zip extract zip into a folder/ save a folder into a zip file...
    回應