Questions tagged [object-lifetime]

The object lifetime (or life cycle) of an object in object-oriented programming is the time between an object is created (also known as instantiation or construction) till the object is no longer used and then destructed or freed.

In typical case, the process is as follows:

  1. Memory allocation and binding: Calculate the size of the object (usually the alligned size of each member of the object), allocating memory space with the size of an object plus the growth later, if possible to know in advance and binding methods.
  2. Constructor call and execution: First the constructor of superclass(es) are called, then the constructor of the object.
  3. Object use.
  4. Destructor call and execution: First the destructor of the object are called, then the destructor(s) of the superclass(es).
  5. Memory deallocation and unbinding.
402 questions
11
votes
1 answer

Extending temporary's lifetime, works with block-scoped aggregate, but not through `new`; why?

Note: This question was originally asked as a comment by Ryan Haining on this answer. struct A { std::string const& ref; }; // (1) A a { "hello world" }; // temporary's lifetime is extended to that of `a` std::cout << a.ref <<…
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
11
votes
4 answers

Will a reference bound to a function parameter prolong the lifetime of that temporary?

I have this code (simplified version): const int& function( const int& param ) { return param; } const int& reference = function( 10 ); //use reference I can't quite decide to which extent C++03 Standard $12.2/5 wording The temporary to…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
10
votes
2 answers

What is the lifetime for a variable stored in service worker?

For example i declare a global variable in service worker like var cacheVersion = "v1"; // line no 1 in sw.js what is the lifetime for this variable?. will this variable be there until service worker gets unregistered or will it be deleted if the…
LiterallyNoOne
  • 101
  • 1
  • 5
10
votes
4 answers

Why isn't it undefined behaviour to destroy an object that was overwritten by placement new?

I'm trying to figure out whether the following is undefined behaviour. I have a feeling it's not UB, but my reading of the standard makes it look like it is UB: #include struct A { A() { std::cout << "1"; } ~A() { std::cout <<…
10
votes
1 answer

Is a const reference bound to another reference which is cast from temporary a dangling reference?

Below is the code snippet: #include using namespace std; struct B{ int b; ~B(){cout <<"destruct B" << endl;} }; B func(){ B b; b.b = 1; return b; } int main(){ const B& instance = (const B&)func(); //is…
choxsword
  • 3,187
  • 18
  • 44
10
votes
2 answers

Destructor call in a comma-separated expression

consider the following example program: #include using namespace std; struct t { ~t() {cout << "destroyed\n"; } }; int main() { cout << "test\n"; t(), cout << "doing stuff\n"; cout << "end\n"; } The output I get with GCC…
10
votes
1 answer

C++: lifetime of an object and external functions

Let's suppose I want to call an external function of my object to perform some checks inside the body constructor. Since the lifetime of an object begins when the constructor's body finishes its execution, is it an unsafe design? struct A; void…
ABu
  • 10,423
  • 6
  • 52
  • 103
10
votes
1 answer

How long does a string constant live in c++?

I've been wondering, how long does a string constant live in C++. For example, if I create some const char *str = "something" inside a function, would it be safe to return the value of str? I wrote a sample program and was really surprised to see…
SiLiKhon
  • 583
  • 4
  • 15
9
votes
2 answers

c++ Dependency Injection: Object lifetimes?

I'm coming from C# and trying to translate some of my practices into C++. I've used dependency injection in various places throughout my code using raw pointers. Then I decide to replace the raw pointers with std::shared_ptr's. As part of that…
User
  • 62,498
  • 72
  • 186
  • 247
9
votes
4 answers

Moving temporary objects into a vector

#include #include #include int i = 0; struct A { A() : j( ++i ) { std::cout<<"constructor "<
BЈовић
  • 62,405
  • 41
  • 173
  • 273
9
votes
2 answers

Variation on the type punning theme: in-place trivial construction

I know this is a pretty common subject, but as much as the typical UB is easy to find, I did not find this variant so far. So, I am trying to formally introduce Pixel objects while avoiding an actual copy of the data. Is this valid? struct Pixel { …
9
votes
3 answers

How to extend lifetime of the local variable or what is right way to use references

I was developing some class and bumped for this question. Consider I have following class: struct A { int *p; A() { p = new int(1); cout << "ctor A" << endl; } A(const A& o) { cout << "copy A" <<…
9
votes
1 answer

Android - is onDestroy supposed to destroy the activity, its variables and free up memory

I have a bug in my code that made me think I don't fully understand the Android Lifecycle. Yes, I have read all the docs and looked at the diagrams, but they seem to talk only about when to save data, when the activity may loose focus or get killed.…
Alex
  • 319
  • 6
  • 14
9
votes
1 answer

Is there any research on (or better use of) of RAII in GC languages?

Note: Object Lifetime RAII not using/with block scope RAII It seems like its possible using an extra gc category, short lived objects(check gc category somewhat frequently), long lived objects(check gc category less frequently), and resource…
9
votes
2 answers

Can a const int ref in a constructor safely bind to a literal?

I know the standard has an exception about extending the lifetime of temporaries that basically says binding a const reference in a constructor won't extend the lifetime, but does this also apply to literals? For example: class C { private: …
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174