1

I have prepare header file "heap_monitor.h". This header work that, if I include this on some other header file, its check me any memory leak in this header and cpp file. When i forget delete objects in destructor, they stay in heap and this monitor send me error, an I know where i don't delokate memory in heap.

#pragma once
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#define new new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )

#define initHeapMonitor() _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF)

this version of code including crtdbg.h which is Windows library. In Xcode is code I don't compile with this error:

ERROR: 'crtdbg.h' file not found

I must wrote some other code to check this memory leaks or something to fix the program to compile my work without this.

when I delete crtdbg.h x code send me 20 errors in new.cpp: https://prnt.sc/iv1x7d

Know someone how I fix this problem ?

Hajducak Marek
  • 11
  • 1
  • 10
  • 1
    crtdbg.h is a windows thing... you cannot use it on other operating systems, full stop. – user253751 Mar 22 '18 at 22:35
  • 1
    Have you looked into [Valgrind](https://en.wikipedia.org/wiki/Valgrind)? – scohe001 Mar 22 '18 at 22:37
  • Xcode uses a different architecture and provides a different framework than Microsoft. Also see [Instruments: Find Memory Leaks](https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/FindingLeakedMemory.html). Also see [xcode leak detector site:stackoverflow.com](https://www.google.com/search?q=xcode+leak+detector+site:stackoverflow.com). – jww Mar 22 '18 at 22:40
  • I actualy read this: [link](http://valgrind.org), I don't know exactly how this work , and frankly i don't have time to study tools like this. Dont exist some other library to use in xcode without errors ? or same peace of code ? If not I probably must learn to work with Valgrind. – Hajducak Marek Mar 22 '18 at 22:40
  • Okey maybe I don't write exactly. I don't must fined memory leak, I must compile my project to run and test my work. My profesor import me done code to VS and he find all memory leaks. I just want compile my code and I dont know how :( – Hajducak Marek Mar 22 '18 at 22:44
  • "I don't have time to study tools like this." [Install Valgrind](https://stackoverflow.com/questions/40650338/valgrind-on-macos-sierra), compile adding the `-g` flag (ie. `g++ -g main.cpp`), run Valgrind on the executable: `valgrind ./a.out`. No studying necessary! – scohe001 Mar 22 '18 at 23:08
  • If you're just trying to match `new`'s and `delete`'s, [cppcheck](https://en.wikipedia.org/wiki/Cppcheck) will run right on your source code! – scohe001 Mar 22 '18 at 23:09
  • "_ I just want compile my code and I dont know how_" - Your question is about how to find memory leaks, not how to compile the code. Valgrind _is_ the answer - it is non intrusive and does not require code modification or special libraries to be linked. Regardless just writing code that does not leak is not that hard - don't rely on the tools to rescueue you from bad practices - when you write code that allocates, write the code that deallocates at the same time, in a way that guarantees it will happen (in constructor/destructor pairs for example). – Clifford Mar 22 '18 at 23:15
  • @Clifford while I agree with what you're saying about Valgrind (you should write an answer!), if "writing code that does not leak is not that hard" then I don't think we'd have [this](https://www.google.com/search?q=google+chrome+memory+leak&oq=google+chrom+memory+leak&aqs=chrome..69i57j0l5.2927j0j7&sourceid=chrome&ie=UTF-8) or [this](https://www.google.com/search?q=safari+memory+leak&oq=safari+memory+leak&aqs=chrome.0.0l6.2103j0j7&sourceid=chrome&ie=UTF-8) or [this](https://www.google.com/search?q=firefox+memory+leak&oq=firefox+memory+leak&aqs=chrome.0.0l6.3831j0j7&sourceid=chrome&ie=UTF-8). – scohe001 Mar 22 '18 at 23:36
  • @scohe001 : What I have written is an answer to a different question. Valgrind was not first mentioned by me, and I am not qualified to answer - I know of it, but have never used it personally - not a Linux developer. The examples you have cited are for _massive_ and _complex_ code-bases developed my many geographically distributed people, over many years, using third-party libraries of possibly variable quality. From comments this code appears to be school exercises - in that context, it is not that hard. – Clifford Mar 22 '18 at 23:56
  • THX Guys, I use to Valgrind to checks my leaks ;) and I Remove Head_monitor.h and some wrote code in other clases and My program start prefectaly :) – Hajducak Marek Mar 23 '18 at 13:17

2 Answers2

1

As far as I know, the best way to search for memory leaks is to use a tool named Valgrind just use the memcheck command. you can see details here: http://valgrind.org/docs/manual/mc-manual.html for macOS, you can see the thread Valgrind on macOS Sierra

0

http://valgrind.org/downloads/current.html Valgrind is the best method of finding memory leaks for mac c++ development.

Darokrithia
  • 140
  • 2
  • 16