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
0
votes
1 answer

Two objects with arbitrary lifetimes with references to one another?

I want to create two objects A and B, such that A.b() returns a reference to B if it is still alive, otherwise, null, and vice versa for B.a(). Has this problem been solved in a standard or widely-used utility library somewhere before? I've run into…
0
votes
1 answer

How to setup : services.AddSingleton(); with a lifetime of "server start -- till "infinity"" in a Asp.net project

I am uncertain if the problem is on my side (my code) or if the serversetup is responsible for the unwanted behavier. I made a Asp.net single page project with the framework Blazor. It is simular to MVC Asp.net. In my Blazor project I setup a…
nogood
  • 1,117
  • 1
  • 11
  • 34
0
votes
1 answer

What to do if an external crate requires static lifetime explicitly?

I'm using seanmonstar/warp to build my rest service and faced the problem related to lifetimes. Here is how my application starting code looks like: struct MyModelStruct { //... } struct Database { //... } impl Database { fn connect(/*…
Some Name
  • 8,555
  • 5
  • 27
  • 77
0
votes
0 answers

When a service is stopped, should tear-down be necessary?

I'm writing my first .NET service with Visual Studio, and the IDE added a comment inside the OnStop() method: Public Class AFirstService ' [...] Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to…
Amessihel
  • 5,891
  • 3
  • 16
  • 40
0
votes
1 answer

System.AccessViolationException error when stored callback is executed

I have passed as callback a C++ member function to a C# project through a C++/CLI wrapper (this works fine). The C# project is going to call this delegate when receiving data from another .exe process: an event will be raised and a method will call…
0
votes
3 answers

RAII and members if constructor throws?

I've previously worked in a setting where exceptions have been turned off and failed memory allocation means that we kill the program. Now working with exceptions I'm wondering about the precise semantics of the following: class Foo { …
del
  • 1,127
  • 10
  • 16
0
votes
1 answer

Smart pointer concepts ownership and lifetime

There are two concepts (ownership, lifetime) that are important when using C++ smart pointers (unique, shared, weak). I try to understand those concepts and how they influence smart pointer (or raw pointer) usage. I read two rules: Always use smart…
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
0
votes
1 answer

Will garbage collector recycle an object if the object's type was mapped with the TransientLifetimeManager in Unity?

Will garbage collector recycle an object if the object's type was mapped with the TransientLifetimeManager in Unity? I assume the garbage collector will recycle the object. So, that makes me ask the following question: What is the difference between…
0
votes
2 answers

Is object existence distinct from object lifetime?

It may sound philosophical, but it isn't: in C++, can various (classes, scalars) objects exist outside of their lifetime? What is the existence of an object? What is the creation of an object? Is an object created when its lifetime starts? (Edited…
curiousguy
  • 8,038
  • 2
  • 40
  • 58
0
votes
1 answer

Is any use of a union clearly well defined, in any revision of C++?

Consider a simple union with a changed "active member": union U { int i; char *p; }; U u = { 1 }; u.p = 0; Is there any revision of the C++ standard that can properly define what happens here? In particular, what is u.p semantically? It's a…
curiousguy
  • 8,038
  • 2
  • 40
  • 58
0
votes
0 answers

What can I expect about service lifetimes in a Razor Components (Server Side Blazor) App?

I do know the differences between Scoped, Singleton and Transient services lifetimes, but my knowledge is limited to a stateless, MVC application. The root of the confusion starts when I add an EntityFramework context using AddScoped (per-request…
Guilherme
  • 5,143
  • 5
  • 39
  • 60
0
votes
2 answers

Lifetime of objects in c++

class Entity { public: int a; Entity(int t) :a(t) { std::cout << "Constructor !" << std::endl; } ~Entity() { std::cout << "Destructor !" << std::endl; } Entity(Entity& o) { …
Bubesh p
  • 65
  • 1
  • 8
0
votes
0 answers

Lifespan of temporary object passed by reference to function

Yes, similar questions have been asked before but they weren't exactly the same (or at least the answers provided weren't sufficient for me). My general question is: what is the lifespan of a temporary object created while calling a…
NPS
  • 6,003
  • 11
  • 53
  • 90
0
votes
0 answers

Pass a closure as an argument and store it to be called later

I have code similar to the following: pub struct Delay { // This line would also need to change, obviously handle: Option<&'static Fn()>, } impl Delay { pub fn new() -> Delay { Delay { handle: None } } pub fn…
McKayla
  • 6,879
  • 5
  • 36
  • 48
0
votes
3 answers

Unexpected virtual function dispatch when using base class reference instead of pointer

Let say I have a simple class hierarchy as follows with a common api: #include class Base { public: void api() { foo(); } protected: virtual void foo() { std::cout << "Base" <<…
motam79
  • 3,542
  • 5
  • 34
  • 60