Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can references cause memory leaks? [duplicate]

Tags:

c++

c++11

Consider the following C++ code.

struct foo { std::string value; }

inline foo bar() { return { "42" }; }

Now imagine I have a function that uses bar() in the following way.

std::string my_func()
{
    const auto &x = bar();
    return x.value;
}

Does this leak memory Because my_func only holds a reference to x? Or does x still get cleaned up after my_func terminates?

I know this is not how references are supposed to be used. But I just realized this compiles fine and wondered what the semantics of it are.

like image 593
Kai Mast Avatar asked Nov 05 '15 21:11

Kai Mast


People also ask

What is the main cause of memory leaks?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.

Do references occupy memory?

To answer your question in a more general way: the C++ standard does not require that references should occupy memory, but it does not require that they should not. The C++ compiler is free to compile the code in any way, as long as the results are correct, and what they are expected to be.

Which action can cause memory leak?

Causes of Memory LeaksUsing Unwanted Object Reference: These are the object references that are no longer needed. The garbage collector is failed to reclaim the memory because another object still refers to that unwanted object. Using Long-live Static Objects: Using static objects also leads to a memory leak.

What should be avoided to prevent memory leaks?

Causes of Memory Leaks and Their Solutions One should not use static views while developing the application, as static views are never destroyed. One should never use the Context as static, because that context will be available through the life of the application, and will not be restricted to the particular activity.


2 Answers

But I just realized this compiles fine

Code provided should not compile, as trying to assign temporary to lvalue reference.

error: invalid initialization of non-const reference of type ‘foo&’ from an rvalue of type ‘foo’

If you fix code, by

std::string my_func()
{
    const auto &x = bar();
    return x.value;
}

then it would be fine, as const reference extends lifetime of temporary for lifetime of const reference.

like image 144
Slava Avatar answered Oct 06 '22 00:10

Slava


Short answer: no.

Longer answer: In that case, the compiler will make sure, that the referenced temporary object will live to the end of the current scope. bar() returns n object by value. That will be copied into a temporary, anonymous object and the reference will then reference that temporary object.

There are other situations similar to that, where the standard has this explicit requirement: temporaries that are bound to references live until the end of the current scope is reached.

like image 44
cdonat Avatar answered Oct 06 '22 01:10

cdonat