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

Xamarin, Autofac, NavigationService and BeginLifetimeScope

A beginner question on lifetimescopes with autofac and when to use them, in a xamarin app. As mentioned in this article (https://nblumhardt.com/2011/01/an-autofac-lifetime-primer/) which is referred to by the autofac documentation…
Steven
  • 335
  • 1
  • 3
  • 13
5
votes
4 answers

Why can I reinitialize a constant inside a loop?

The compiler throws no warnings or errors for the following code. Is the meaning of the const qualifier being abused? Obviously I cannot reassign it later in the same loop iteration but it does seem to reassign it after each iteration. Sample…
user393454
  • 139
  • 1
  • 7
5
votes
1 answer

Smart pointer that lazily recreates its resource

I have a ServiceProvider class which contains a couple of pointers to different services, like that: class ServiceProvider() { Service3* GetService3(); public: void Process(Object* o); void Shrink(); private: …
Netherwire
  • 2,669
  • 3
  • 31
  • 54
5
votes
3 answers

The lifetime of a temporary to which several references are bound in C++

The C++ standard draft N4296 says [class.temporary/5] The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference…
xskxzr
  • 12,442
  • 12
  • 37
  • 77
5
votes
5 answers

Variable lifetime

What happends to variable when line of execution goes outside of code block? For example: 1 public void myMethod() 2 { 3 int number; 4 number = 5; 5 } so, we declare and set variable. When it goes outside of code block (line 5) what…
ExpertLoser
  • 257
  • 4
  • 14
5
votes
1 answer

can memcpy for std::aligned_storage?

std::aligned_storage::type is POD type. POD type can memcpy. However, What happens if placement new non-trivially-copyable type to std::aligned_storage? Can it memcpy that std::aligned_storage? non-trivially-copyable type(non-POD type) can NOT…
Cocoa
  • 95
  • 4
5
votes
2 answers

Is a reference returned from a temporary variable valid?

I've come across a situation where being able to chain a method call to a temporary variable would be really helpful: draw(Quad(0, 0, 1, 1).rotate(90)); // <-- .rotate() returns a Quad reference struct Quad{ Quad(float x, float y, float width,…
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
5
votes
1 answer

Reusing a jQuery object is faster, but is it always better?

I ran across some code at work as follows: var $divs = $('div'); var $jq = $([1]); $divs.each(function () { $jq.context = $jq[0] = this; // ... do stuff ... }); I perf'd the above, and it seems much faster than having $this = $(this); at…
Impirator
  • 1,393
  • 1
  • 12
  • 24
5
votes
1 answer

does passing a method of one object to another object keep the first object alive?

Suppose I have three objects: 'a', 'b' and 'c'. Object 'a' and 'c' are long-lived, statically referenced service singletons. Object 'b' is short-lived, i.e. no static references keep it alive. Now suppose object 'a' creates an instance of object 'b'…
nerophon
  • 89
  • 1
  • 10
5
votes
3 answers

C++ lambda. How need to capture the pointer?

For example, there is a method: void foo( A* p ) { auto l = [=](){ /* Do Something using p */ }; // Use l ... } How I should to capture the pointer: by reference or by value? Inside lambda p is not changed, just used its methods.
kaa
  • 1,265
  • 5
  • 22
  • 39
5
votes
3 answers

In D, is it possible for an object to hold a member object internally?

In D, it's possible to allocate classes on the stack using scope, i.e. void foo() { scope example = new Bar(); } Now, if class Foo has class Bar as a member, is there any way to store Bar in-place inside Foo and have it tear down with Foo a la…
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
5
votes
3 answers

Reference to an unnamed temporary object (life time)

After reading this answer from ildjarn, I wrote the following example, and it looks like an unnamed temporary object has the same life time as its reference! How come this is possible? Is it specified in the C++ standard? Which version? Source…
oHo
  • 51,447
  • 27
  • 165
  • 200
5
votes
3 answers

What is the order of calling destructors for temporaries in C++?

Consider the following code: #include struct A { ~A() { std::cout << "~A" << std::endl; } }; struct B { ~B() { std::cout << "~B" << std::endl; } }; struct C { ~C() { std::cout << "~C" << std::endl; } void operator<<(const B &)…
vitaut
  • 49,672
  • 25
  • 199
  • 336
5
votes
2 answers

How do the ISponsor and ILease interfaces work?

I've created a object that inherits from MarshalByRefObject and ISponsor. In my implementation of ISponsor I just return a timespan to indicate how long I want the object renewed for. When I call InitializeLifetimeService() to get an ILease…
dmck
  • 7,801
  • 7
  • 43
  • 79
4
votes
3 answers

Boost shared_from_this and destructor

I found that it is not allowed to call shared_from_this in the destructor from a class: https://svn.boost.org/trac/boost/ticket/147 This behavior is by design. Since the destructor will destroy the object, it is not safe to create a shared_ptr to…
Allan
  • 4,562
  • 8
  • 38
  • 59