0

I read the following article to get a better understanding of the Stack vs Heap: http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html

How can one manually test their C++ code for memory leaks without using an automated memory leak tool? Since variables on the stack are managed, I assume that a memory leak can only occur when objects are created on the heap.

I apologize. I could only think of two examples. which of the two is better and/or is there a better way to manually observe memory?

Given:

int COUNTERS::NEW_COUNTER = 0;
int COUNTERS::DELETE_COUNTER = 0;
int COUNTERS::CONSTRUCTOR_COUNTER = 0;
int COUNTERS::DESTRUCTOR_COUNTER = 0;

1) Counting Constructor and Destructor counts:

class Example
{
    public:
        Example()
        {
            COUNTERS::CONSTRUCTOR_COUNTER++;
        }

        ~Example()
        {
            COUNTERS::DESTRUCTOR_COUNTER++;
        }
};

2) Counting New and Delete operators:

void myExampleFunction()
{
    Example* e = new Example();
    COUNTERS::NEW_COUNTER++;

    // do stuff with e

    delete e;
    COUNTERS::DELETE_COUNTER++;
}

Thank you.

code
  • 5,294
  • 16
  • 62
  • 113
  • if using counters, you need also consider copy constructor and the assignment operator. But what's the purpose of this? Is there any real problem? – Matt Jul 29 '14 at 18:54
  • Hi @Matt, sorry for the stupid question. Could you give an example why the copy constructor and assignment operator should be considered? – code Jul 29 '14 at 21:28
  • because new instances are created – Matt Jul 30 '14 at 12:38

1 Answers1

1

Note: Whenever you define the destructor in c++, you must define the big 5, meaning the destructor, the copy constructor, copy assignment, move constructor, move assignment. There are many examples of this online of how to do this properly.

http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)

First the standard warning goes out, any object that does not be created on the heap should not be created because it innately has the possibility for a leak. Meaning, you should use references, like int& a; stack variables; or return methods by value, like int instead of int&

Otherwise I would try to avoid having to create your own classes in order to keep track of variables. Use something like std::shared_ptr or std::unique_ptr as much as possible because unique_ptr will only allow one reference while shared_ptr will count. As @quantdev said, take a look at that example as it is very insightful. I recommend keeping track of your variables for small programs, but use a garbage collector, tons of articles about the different types online, for your larger program.

As far as your question goes, the first method would be better, but please do not put that in all of your classes because that would be a horrendous amount of code duplication. The second method -- I guarantee you -- you will forget, someone may not read the documentation, one of another million things can go wrong. Obviously, expanding this question out of its original scope, there are many other ways to reduce memory leaks, but that is neither here nor there.

halfer
  • 19,824
  • 17
  • 99
  • 186
bhuvy
  • 304
  • 2
  • 7