Questions tagged [lifetime]

A variable's life-time is the time during which the variable is bound to a specific memory location. The life time starts when the variable is allocated and ends when it is deallocated.

Most of the time a lifetime is a synonym for the scope, for example in this code:

void foo()
{
    int x = 0; // lifetime of `x' begins here ──┐
    //                                          │
    printf("%d\n", x); //                       │
} // and finishes here ─────────────────────────┘

Rust:

The lifetime is a key concept in : it is a construct the compiler (also called the ) uses to ensure all borrows are valid.

Links:

2138 questions
49
votes
6 answers

Revive object from destructor in C++?

Disclaimer: I know this is bad design, I am simply asking the question out of curiosity in order to try to obtain deeper knowledge of how the destructor works in C++. In C#, one can write: GC.KeepAlive(this) in the destructor of a class (see edit…
MathuSum Mut
  • 2,765
  • 3
  • 27
  • 59
48
votes
4 answers

How do I write an iterator that returns references to itself?

I am having trouble expressing the lifetime of the return value of an Iterator implementation. How can I compile this code without changing the return value of the iterator? I'd like it to return a vector of references. It is obvious that I am not…
elszben
  • 495
  • 1
  • 4
  • 6
45
votes
1 answer

How can I create my own data structure with an iterator that returns mutable references?

I have created a data structure in Rust and I want to create iterators for it. Immutable iterators are easy enough. I currently have this, and it works fine: // This is a mock of the "real" EdgeIndexes class as // the one in my real program is…
Mike Pedersen
  • 1,056
  • 10
  • 18
45
votes
3 answers

Singleton Per Call Context (Web Request) in Unity

A few days ago, I had an issue with ASP.Net threading. I wanted to have a singleton object per web request. I actually need this for my unit of work. I wanted to instantiate a unit of work per web request so that identity map is valid through out…
Mehdi Khalili
  • 927
  • 1
  • 11
  • 18
42
votes
4 answers

Lambda passed by reference runs when invoked in the constructor, but not when later stored in a data member

The following C++ code prints 11.1 then crashes. The lambda function seems to be called correctly inside the constructor, but then later, that same function no longer works! Why is this happening? Does the lambda have a limited lifespan? #include…
Rocketmagnet
  • 5,656
  • 8
  • 36
  • 47
42
votes
2 answers

What is the meaning of 'a: 'a in generic lifetime parameters?

I have a strange piece of code: #![allow(unused)] fn f<'a>() {} fn g<'a: 'a>() {} fn main() { // let pf = f::<'static> as fn(); // (7) let pg = g::<'static> as fn(); // (8) //print!("{}", pf == pg); } The 7th line cannot be…
iDuanYingJie
  • 663
  • 4
  • 11
42
votes
8 answers

Malloc and constructors

Unlike new and delete expressions, std::malloc does not call the constructor when memory for an object is allocated. In that case, how must we create an object so that the constructor will also be called?
ckv
  • 10,539
  • 20
  • 100
  • 144
42
votes
5 answers

What should be the lifetime of an NHibernate session?

I'm new to NHibernate, and have seen some issues when closing sessions prematurely. I've solved this temporarily by reusing sessions instead of opening a session per transaction. However, I was under the impression that opening sessions each time…
stiank81
  • 25,418
  • 43
  • 131
  • 202
40
votes
1 answer

Value does not live long enough

I don't completely understand lifetimes, but I think b's lifetime will end before self's. So, how to edit this code? Do I copy something in memory? If I make a new instance, this lifetime must adhere to this case. pub struct Formater { layout:…
彭灵俊
  • 403
  • 1
  • 4
  • 5
37
votes
2 answers

C++ - using const reference to prolong a member of a temporary, ok or UB?

consider something like this: #include struct C { C(double x=0, double y=0): x(x) , y(y) { std::cout << "C ctor " << x << " " <
user2717954
  • 1,822
  • 2
  • 17
  • 28
37
votes
2 answers

Lifetime of temporaries

The following code works fine, but why is this correct code? Why is the "c_str()" pointer of the temporary returned by foo() valid? I thought, that this temporary is already destroyed when bar() is entered - but it doesn't seem to be like this. So,…
Frunsi
  • 7,099
  • 5
  • 36
  • 42
37
votes
2 answers

lifetime of a std::initializer_list return value

GCC's implementation destroys a std::initializer_list array returned from a function at the end of the return full-expression. Is this correct? Both test cases in this program show the destructors executing before the value can be used: #include…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
35
votes
1 answer

Cannot infer an appropriate lifetime for autoref due to conflicting requirements

I'm having lifetime issues with a particular function in my code. I'm following a tutorial in an attempt to learn Rust and SDL. The tutorial was slightly older and the SDL library has changed since its been written, so I'm following along while…
Brad Ziolko
  • 355
  • 1
  • 3
  • 6
35
votes
4 answers

Lifetime of a string literal returned by a function

Consider this code: const char* someFun() { // ... some stuff return "Some text!!" } int main() { { // Block: A const char* retStr = someFun(); // use retStr } } In the function someFun(), where is "Some text!!" stored (I…
sud03r
  • 19,109
  • 16
  • 77
  • 96
34
votes
2 answers

Is there a way to have a Rust closure that moves only some variables into it?

I have a general struct with settings and an extra variable setting that I want to tune and play around with. For all possible values in an integer range, I want to start a (scoped) thread with this variable set to that value. Depending on this…
Qqwy
  • 5,214
  • 5
  • 42
  • 83