Ways to avoid Memory Leaks in C/C++

前端 未结 6 770
面向向阳花
面向向阳花 2020-12-25 09:18

What are some tips I can use to avoid memory leaks in my applications? In my current project I use a tool \"INSURE++\" which finds the memory leak and generate the report. <

6条回答
  •  旧时难觅i
    2020-12-25 10:06

    Smart pointers can be very helpful in automating the bookkeeping of object lifetimes:

    http://ootips.org/yonat/4dev/smart-pointers.html

    Where possible, use stack allocated objects inside of their relevant scopes instead of new/delete.

    Tools like valgrind have some overhead and can slow down your runs. If you know your codebase and the kinds of leaks that tend to arise, you can target specific classes and implement lighter weight checks (even just a simple object count that you check against zero when you quit). These lightweight checks can then be used to motivate you into doing a more extensive valgrind debugging session when they are triggered.

提交回复
热议问题