Most of memory leaks appear when a pointer of an object returned and programmer forgot to delete it.
for example:
class my_class
{
...
};
my_class* func1()
{
my_class* c = new my_class;
return c;
}
int main()
{
my_class* var1 = func1();
...
// Programmer forgot delete the var1: delete var1;
// -- or --
// Doesn't know 'delete[] var1;' is correct or 'delete var1;'.
}
Some of memory leaks appear when a pointer to an object created and programmer forgot to delete it.
for example:
class my_class
{
...
};
void func2(my_class* p)
{
...
}
int main()
{
my_class* var3 = new my_class;
func2(var3);
// Does func2 deletes var3? Programmer doesn't know.
// -- or --
// Programmer forgot delete the var3.
}
I use a method to resolve memory leaks but I don't sure about it in complex situations.
My method is: Don't use any pointers (except one place), Just use references instead of pointers.
for example:
class my_class
{
...
};
my_class& func1()
{
my_class* c = new my_class; // except one place.
return *c;
}
void func2(my_class& p)
{
...
}
int main()
{
my_class& var1 = func1();
my_class var2 = func1();
my_class var3;
func2(var3);
// There is nothing to forget.
}
Does using references instead of pointers, resolve memory leaks?
Is it a good method for resolving memory leaks or there are better methods?
Edit:
Some answer of this question don't agree the below code don't have memory leak.
because it is a new question, I ask it seperately.
class my_class
{
...
};
my_class& func()
{
my_class* c = new my_class;
return *c;
}
int main()
{
my_class& var1 = func();
// I think there is no memory leak.
}
I ask it here: Does this code leak memory? (references, new, but no delete)
You shouldn't replace an owning pointer (one that is responsible for deletion) with a reference; it's idiomatic to assume that references never, ever own the referred resource.
Instead, replace owning pointers by smart pointers: std::unique_ptr func1() { return std::unique_ptr(new my_class); }
You are correct that non-owning pointers can be replaced by references. It is recommended for most cases (see the link at end for more information).
Here is a question regarding the difference of uses between raw pointers and smart pointers. In an answer of mine I mention difference use cases for raw pointers vs references.
Yes there is a memory leak. The following code is executed with
valgrind
:Don't return raw pointers from functions; stick them in a smart pointer class such as
unique_ptr
orshared_ptr
. Then you don't have to worry about deleting the allocated object.Also, in your second example, who is deleting the object allocated by
func1()
? Just because you return a reference instead of pointer doesn't mean freeing of allocated memory will happen magically.You haven't resolved any memory leaks. If you new, then you must delete. All you did was dereference the pointer, it still needs to be deleted. You can resolve memory leaks by creating local objects and returning by value, or using smart pointers. 99 times out of 100, I prefer the return by value option.
Now, like many beginners, the idea of returning large objects by value probably scares your perf-centric mind. Read this to allay your fears.
Your approach does not help at all:
You are still just allocating the object dynamically and have to take care of it manually! In fact, my first usage makes a copy of the reference and then forgets the reference, creating an instant leak! And even if you did still somehow retain the reference, as in the second example, it becomes entirely unmanageable! (See Soap's comment.)
So please just forget about this entire idea quickly and look at resource managing containers instead!
For example: