How can i make sure I delete my array, my destructor is not being called and now i have memory leak.
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
cout << " Enter the size of the array : ";
int size_arr;
cin >> size_arr;
{
Array arr(size_arr);
} //force scope
system("pause"); //otherwise this happens
return 0;
} //before destructor is called down here...
What do you mean by forcing a scope? Is that a regular solution, or is that a last resort solution? @Jonnin I dont think planting brackets just like that is a very good idea...
@Last chance, we are not doing copy constructors just yet, they are not part of this project. I have no worked on the other functions but they do not matter as of right now, because all they do is manipulate the data of the array like reversing, etc.
I know there is memory leak because i am allocating memory in the heap by using new, and my destructor is not being called
int main()
{
std::cout << " Enter the size of the array: ";
int size_arr;
std::cin >> size_arr;
Arr arr(size_arr);
Arr brr;
return 0;
}
Enter the size of the array: 2
Overloaded constructor called
Default constructor called
Destructor called
Destructor called
Program ended with exit code: 0
#include <iostream>
usingnamespace std;
class Arr
{
int size;
double *pArr;
public:
Arr( int arr_size )
{
size = arr_size;
if ( size == 0 )
{
create_arry();
}
else
{
pArr = newdouble[size];
}
cout << "Constructor called with size " << size << '\n';
}
~Arr()
{
cout << "Destructor is called\n";
delete[] pArr;
}
void create_arry()
{
size = 6;
pArr = newdouble[size];
}
};
int main()
{
cout << "Enter the size of the array: ";
int size_arr;
cin >> size_arr;
Arr arr(size_arr);
}
Enter the size of the array: 10
Constructor called with size 10
Destructor is Called
rnima wrote:
I know there is memory leak because i am allocating memory in the heap by using new, and my destructor is not being called
Then you know wrong.
rnima wrote:
I dont think planting brackets just like that is a very good idea...
I think it is an excellent idea. When it gets to the end of that limited scope, anything created within that scope will end its lifetime and have its destructor called.
Enter the size of the array: 9
Overloaded constructor called
Default constructor called
Default constructor called
pArr deleted
Destructor called
Destructor called
pArr deleted
Destructor called
Program ended with exit code: 0
I dont think planting brackets just like that is a very good idea..
I said why in the comments.
The brackets show that your destructor is working; they are not useful in a real program. You are not totally wrong here, but it shows you what you needed to see with the least amount of surgery. You should take them back out once you see what you need to see with them in.
What I am seeing is you not run the program from a console window, and instead relying on system pause from the gui. This is ok but you can't see the destructor being called this way because it is called after the pause ended. It is working fine, you just can't see it because of how you choose to run your program.
Introducing scope in the middle of a function as above is extremely useful and it's something I personally do frequently. It's a great way to ensure an object will be destructed at a specific point without having to introduce a new function and pass everything I need to it. The simplest example I can think of is
Besides locks, there's other objects where order of destruction may be critical, such as streams, network connections, database connections, and global system objects.