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
3
votes
4 answers

What other IoC containers have an IInitializable like feature?

I've been using Castle Windsor in my previous project and I liked it a lot. For my current project I'm looking to use a different IoC container. Castle Windsor hasn't had any new releases since 2007 and is still not at version 1.0 so it is hard to…
3
votes
2 answers

Can I override Dispose to make an entity class that always calls SaveChanges?

This is a fairly fine point, and I expect the answer is "it's not a hot idea to begin with" - that said, it has a points that I'm interested in regardless, if someone is kind enough to indulge. Model Code: public partial class MyEntities :…
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
3
votes
2 answers

Is it UB to keep a pointer to a destroyed object and then use it to access re-created objects, possibly pointing to the first subobject?

Consider struct full { struct basic { int a = 1; } base; int b = 2; }; void example() { alignas(full) std::byte storage[/* plenty of storage */]; full * pf = new (storage) full; basic * pb = &pf->base; new…
3
votes
1 answer

How do I fix the lifetime mismatch when returning a mutable reference to the struct's field from a trait method?

This is my attempt to return a mutable reference to the struct's field. pub trait Objective { fn get_children<'a>(&'a mut self) -> &'_ mut Vec<&'_ mut Box>; fn get_parent(&'_ mut self) -> &'_ mut Box; fn…
Tr3nchHvlk
  • 137
  • 1
  • 6
3
votes
2 answers

Reusing data member storage via placement new during enclosing object's lifetime

This is a follow-up to my previous question where I seem to have made the problem more involved than I had originally intended. (See discussions in question and answer comments there.) This question is a slight modification of the original question…
walnut
  • 21,629
  • 4
  • 23
  • 59
3
votes
1 answer

What is the lifetime of unassigned object created in a constructor in java?

I have a class(A) with constructor defined as shown below. In the constructor, I have created an object for B by passing a listener(interface) implementation to it as shown below. public class A { private String str; public A() { …
Vamshi
  • 133
  • 1
  • 9
3
votes
1 answer

C++ -- where does the system store the returned characters?

#include "stdafx.h" #include #include using namespace std; const char* funA() { return "aa"; // where does system to store this temporary variable? } // this is not an valid function const char* funB() { string…
q0987
  • 34,938
  • 69
  • 242
  • 387
3
votes
2 answers

c++17: A temporary object never destroyed

struct Base { Base() { std::cout << "Inside: " << __PRETTY_FUNCTION__ << std::endl; } ~Base() { std::cout << "Inside: " << __PRETTY_FUNCTION__ << std::endl; } }; struct BaseWrapper { const Base &b; }; int…
Yang Yang
  • 411
  • 3
  • 11
3
votes
3 answers

Lifetime of object which has vacuous initialization

Current draft standard says (previous standards have similar wording) in [basic.life/1]: The lifetime of an object or reference is a runtime property of the object or reference. An object is said to have non-vacuous initialization if it is of a…
geza
  • 28,403
  • 6
  • 61
  • 135
3
votes
2 answers

Singelton lifetime within a dll / bundle

If I create a singleton class in the context of a dll or bundle on mac, the singleton class is instantiated once and used by all instances of the dll. I am using a dll as a plug-in for an application. Now the following thing came to my mind: If I…
st-h
  • 2,444
  • 6
  • 37
  • 60
3
votes
2 answers

Factory pattern and lifetime of injected dependencies dilemma

This has been bothering me for a long time, and I could not find the right answer. The Problem. Imagine you have a factory interface (C# example): interface IFooFactory { IFoo Create(); } and it's implementation depends on a service: class…
Serge Semenov
  • 9,232
  • 3
  • 23
  • 24
3
votes
2 answers

Lifetime of a temporary with temporary subexpressions, bound to a reference

Is the following use of p within main safe? I believe binding the temporary produced by mk_pair has its lifetime extended to that of p, but how about the temporary objects created by Wrap{1} and Wrap{2}? struct Wrap { int &&x; }; struct Pair { Wrap…
user2023370
  • 10,488
  • 6
  • 50
  • 83
3
votes
2 answers

How do I elegantly use smart pointers to model complex lifetimes in C++?

Suppose I have a collection of Foo objects, and each Foo owns one or more Bar objects. A specific Foo or Bar can be deleted by a user of my interface; when a Foo is deleted, all the Bars it owns are deleted too. So far, giving each Foo a collection…
Yaron Tausky
  • 803
  • 2
  • 8
  • 12
3
votes
2 answers

What is the difference between a destructor and a funtion that deallocates?

What is the difference between the destructor and the DeAllocate function in this code? I don't get it they look like the same thing to me.They do the exact same thing, why would you ever need a function like DeAllocate? Destructor has the benefit…
Matt
  • 2,232
  • 8
  • 35
  • 64
3
votes
2 answers

How to manage lifetime of the type "shortest of"?

We use composition, when an object has a single parent, which should care for the object's lifetime. We use unique_ptr when in the same situation, but the object can be nullptr. We use shared_ptr when several external entities may need our object,…
Vorac
  • 8,726
  • 11
  • 58
  • 101