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
2
votes
2 answers

How can I ensure Dispose() is called on an advanced function's local variable on stop signal?

I have noticed that objects implementing IDisposable in advanced functions aren't reliably disposed of when a "stop" signal (eg. pressing CTRL+C) is sent during execution. This is a pain when the object holds a handle to, for example, a file. If…
alx9r
  • 3,675
  • 4
  • 26
  • 55
2
votes
4 answers

Why can't I dynamic_cast "sideways" during multiple inheritence?

The following code throws std::bad_cast struct Foo { void foo () {} }; struct Bar { Bar () { dynamic_cast (*this) .foo (); } virtual ~ Bar () {} }; struct Baz : public Foo, public Bar { }; int main () { Baz…
spraff
  • 32,570
  • 22
  • 121
  • 229
2
votes
3 answers

How do I call a DAO method from Service layer in a J2EE web application

Generally a lot of applications these days use Spring which takes care of the life cycle of the pojo classes in their application. But what if my application cannot use Spring due to some other issues. How do I go from the Service layer of the…
Arunabh
  • 167
  • 1
  • 7
  • 18
2
votes
1 answer

OOP PHP confusion with how data is stored

Recently, I have been trying to object orient my PHP. I understand OOP, but for some reason, I am having trouble when it comes to implementing it with Php. I feel as though it isn't making sense. For instance, lets say I have a Friend class. It has…
Wyatt
  • 445
  • 3
  • 12
2
votes
2 answers

Reference to anonymous rvalue is corrupted

Why does the following code... #include #include template< typename T, typename U > class Map { public: Map( const T& t, const U& u ) { map_[ t ] = u; } Map< T, U >& operator() ( const T& t, const U& u ) { map_[ t ] = u; …
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
2
votes
1 answer

What happens when a function returns a CString?

I have a very thorough understanding of memory and pointers, but I need a little refresher with regard to exactly how C++ manages some objects under the covers. Consider the following code: void Test() { LPCTSTR psz =…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
2
votes
3 answers

C++ objects lifetime on stack and heap

I'm trying to translate some projects I've made with Delphi; an object can be declared in general as: //I have the control of the object and I MUST delete it when it's not needed anymore male := THuman.Create(); try // code finally male.Free;…
Raffaele Rossi
  • 2,997
  • 4
  • 31
  • 62
2
votes
1 answer

Enforce lifetime of parent object

Consider the following code: class Child { vector::iterator it; } class Parent { vector objects; Child getChild(int idx) { Child ret; ret.it = objects.begin() + idx; return ret; } } Obviously the…
Managarm
  • 1,070
  • 3
  • 12
  • 25
2
votes
6 answers

Preventing early object destruction

In C++, if I write token make_token() { return token{}; } and then use it as follows void use_token() { make_token(); // extra code } without assigning a token to a variable, token's destructor fires before extra code executes. How can I get…
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
2
votes
1 answer

Are local objects guaranteed to outlive temporary arguments? (C++11)

If I have a setup where a function takes an object Bar as an argument, passes that object to a local class Foo, and Foo uses the Bar in its destructor such as: class Foo { public: Foo(const Bar& bar) : bar_(bar) {} ~Foo() { …
mfsiega
  • 2,852
  • 19
  • 22
2
votes
1 answer

adapter class for C++ interface with move constructor

I am trying to write an adapter class for an interface class that accepts a) an implementation of the interface, which should be stack-allocated (so no new/delete handling should be required from the outside, the adapter itself may use new/delete)…
Niklas R
  • 16,299
  • 28
  • 108
  • 203
2
votes
4 answers

Why is unique_ptr null?

In the code snippet below, the assertion in foo always fires. Can anyone explain why y is a nullptr? It looks like a lifetime issue, i.e. y is destroyed between the calls to put and get but I don't really understand why. What am I…
ksl
  • 4,519
  • 11
  • 65
  • 106
2
votes
1 answer

Is there a reliable way to destroy private data structures when a standard NSView or NSWindow is destroyed?

I am developing a GUI framework for another programming language which lets me target native backends, namely the Windows API on Windows, Cocoa on Mac OS X, and GTK+ on other Unix systems. The actual guts of the framework will still be in C and…
andlabs
  • 11,290
  • 1
  • 31
  • 52
2
votes
1 answer

How To Implement PerGraph Lifestyle

According to Simple Injector documentation, a Per Graph lifestyle exists that can be used to limit the scoping of an object to the current graph. But the documentation doesn't list what actually needs done to implement it, and I can't find any…
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
2
votes
1 answer

Is HierarchicalLifetimeManager the correct lifetime manager to use for WCF client / ChannelFactory

I have a very simple WPF application that is calling There is one container for the application, that is configured when the application starts. The WPF app has a service reference to my WCF service. I have registered my generated proxy to the…