• Forum
  • Lounge
  • Memory leak through careless static use.

 
Memory leak through careless static use...

Here is my "genial" memory leak scheme by which my program leaked to virtual (memory) death:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Selection
{
public:
  int* elements;
  int size;
  Selection(){elements=0;};
  void init(int NumElements);
  ~Selection(){delete [] elements;};
};

void Selection::init(int NumElements)
{
  this->elements = new int[NumElements]
  this->size = NumElements;
}

int myFunctionLeaking()
{
  static Selection Sel;
  Sel.init(10);
  return Selection.size;
}//how I leaked [Edit:] lots of RAM...

int myFunction()
{
  Selection Sel;
  Sel.init(10);
  return Selection.size;
}//no memory leak here 
Last edited on
Was there something particularly wrong with using the constructor to initialize the object?
Since you are asking, I originally read somewhere (perhaps in this forum?) that, when using the new operator, it is a good idea to do it outside of the constructor, since new could fail.

Later however, I found out that I really don't want my program to continue execution if my "new" fails. I also found out that I dont really wanna do error catching code, since the system does well enough on informing you you messed up :).

However, the "init" functions remained in my code. The reason there was no "delete" before the "new" in my "init" function (like there always should be) was that I hadn't figured that out in the "restart" (asuming it ever began) of my c++ "carier" a couple of months ago...
Last edited on
It is perfectly fine for the constructor to crash, you would just throw an exception at that point. However, the deconstructor shouldn't ever throw.
closed account (z05DSL3A)
Exception and constructors is a tricky one.

If you have code such as:
1
2
3
4
5
6
7
8
class foo
{
   int* ptr;
   void init(); // here to show some init is done on constructor
public:
    foo(int s) {ptr =new int[s]; init();}
    ~foo() {delete[] ptr;}
}


If an exception is thrown out of the constructor, then the foo object is not fully formed so it's destructor will not be called and the memory will be leaked.

You can use a self contained container class such as a vector:
1
2
3
4
5
6
7
8
class bar
{
   vector<int> p;
   void init(); // here to show some init is done on constructor
public:
    bar(int s) : p(s){init();}
    //...
}

That way as long as p is fully formed, if the construction of bar craps out, p will be deleted successfully.

I dont understand the syntax at line 6. Can you point a link to me where the use of bar(int s) : p(s){init();} is explained?
closed account (z05DSL3A)
1
2
3
4
5
bar(int s) 
: p(s)      // initialization list
{
    init();
} 


This may help:
C++ General: What is the initialization list and why should I use it?
http://www.codeguru.com/forum/showthread.php?t=464084
Hmmm I usually avoid posting regarding memory leaks. However.

1
2
  this->elements = new int[NumElements]
  this->size = NumElements;

You should do a check first. if (elements != 0) delete [] elements; incase init() is called multiple times.

1
2
3
  static Selection Sel;
  Sel.init(10);
  return Selection.size;

Will leak on each call because init() is not checking to see if elements has already been declared, and if so; freeing the memory.

~Selection(){delete [] elements;};
You should check to ensure elements != 0 before deleting the memory. Otherwise you may end up with a crash if your new has failed or init() has no been called.
thanks Zaita, I actually learned what you advise me to do by experience a month or two ago. Unfortunately I didn't check my older code...
Last edited on
Topic archived. No new replies allowed.