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
16
votes
6 answers

checking invariants in C++

Are there any established patterns for checking class invariants in C++? Ideally, the invariants would be automatically checked at the beginning and at the end of each public member function. As far as I know, C with classes provided special before…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
16
votes
9 answers

A Question On Smart Pointers and Their Inevitable Indeterminism

I've been extensively using smart pointers (boost::shared_ptr to be exact) in my projects for the last two years. I understand and appreciate their benefits and I generally like them a lot. But the more I use them, the more I miss the deterministic…
Ash
  • 547
  • 5
  • 13
15
votes
2 answers

Is it safe to pass arguments by reference into a std::thread function?

#include #include #include #include using namespace std; void f(const vector& coll) { this_thread::sleep_for(1h); // // Is coll guaranteed to be valid before exiting this function? …
xmllmx
  • 39,765
  • 26
  • 162
  • 323
15
votes
1 answer

Extending temporary's lifetime through rvalue data-member works with aggregate, but not with constructor, why?

I've found the following scheme to extend a temporaries lifetime works, I don't know if it should, but it does. struct S { std::vector&& vec; }; int main() { S s1{std::vector(5)}; // construct with temporary std::cout <<…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
14
votes
4 answers

MEF keeps reference of NonShared IDisposable parts, not allowing them to be collected by GC

I've encountered somewhat of a problem in MEF's part lifetime which causes memory leaks in my Prism application. My application exports views and viewmodels with the PartCreationPolicy being set to CreationPolicy.NonShared. The views and viewmodels…
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
14
votes
3 answers

Is using the result of new char[] or malloc to casted float * is UB (strict aliasing violation)?

Which code of these has UB (specifically, which violates strict aliasing rule)? void a() { std::vector v(sizeof(float)); float *f = reinterpret_cast(v.data()); *f = 42; } void b() { char *a = new char[sizeof(float)]; …
geza
  • 28,403
  • 6
  • 61
  • 135
13
votes
4 answers

Lifetime of object is over before destructor is called?

I don't understand this: 3.8/1 "The lifetime of an object of type T ends when: — if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or — the storage which the object occupies is reused or released." If the…
Pubby
  • 51,882
  • 13
  • 139
  • 180
13
votes
4 answers

Is virtual table creation thread safe?

Please let me begin with that I know it is a bad practice to call virtual functions from within a constructor/destructor. However, the behavior in doing so, although it might be confusing or not what the user is expecting, is still well…
Gils
  • 478
  • 2
  • 9
13
votes
1 answer

Manually constructing a trivial base class via placement-new

Beware, we're skirting the dragon's lair. Consider the following two classes: struct Base { std::string const *str; }; struct Foo : Base { Foo() { std::cout << *str << "\n"; } }; As you can see, I'm accessing an uninitialized pointer. Or…
Quentin
  • 62,093
  • 7
  • 131
  • 191
12
votes
3 answers

Private field captured in anonymous delegate

class A { public event EventHandler AEvent; } class B { private A _foo; private int _bar; public void AttachToAEvent() { _foo.AEvent += delegate() { ... UseBar(_bar); ... } } } Since…
Damir
  • 345
  • 4
  • 16
12
votes
1 answer

What is the relationship between the end of object's lifetime and when it ceases to exist?

In the following short example, what can be said about the object the pointer f points to or used to point to just before returning from main? #include struct foo { std::vector m; }; int main() { auto f = new foo; …
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
12
votes
3 answers

Lifetime of Qt Objects

What are the lifetimes of Qt Objects? Such as: QTcpSocket *socket=new QTcpSocket(); When socket will be destroyed? Should I use delete socket; Is there any difference with: QTcpSocket socket; I couldn't find deep infromation about this, any…
metdos
  • 13,411
  • 17
  • 77
  • 120
12
votes
1 answer

When to use PerThreadLifetimeManager?

I am following the example linked to below for setting up unity to work with my service layer. My project is setup very similar to the one in this article and I understand everything except why exactly is PerThreadLifetimeManager used when…
11
votes
2 answers

What is the lifetime and validity of C++ iterators?

I'm planning to implement a list of Things in C++ where elements might be removed out of order. I don't expect that i'll need any kind of random access (i just need to sweep the list periodically), and the order of items isn't important either. So I…
PypeBros
  • 2,607
  • 24
  • 37
11
votes
3 answers

.NET: Way to determine if object has any references to it?

Q. Is there a way to find out if an object has any "strong references" to it? Raymond Chen hinted that a solution might be possible: You want to know whether the reference count is zero or nonzero. For that, use WeakReference. Notes i have a…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
1 2
3
26 27