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

How to specify a child scope for constructor func factory parameter?

I want to do something like: class MyClass { Func _factory; public MyClass([WithChildScope("OtherClassScope")] Func other) { _factory = other; } public OtherClass LoadOther(int id) { …
Vlad
  • 3,001
  • 1
  • 22
  • 52
0
votes
1 answer

C++: reference and lifetime extension using const

This refers to the C++ programming language. Assume we have a class C and do this: C var1 = C(init_parameters); C& var2(var1); Question 1: If we change var2, for example var2.memberA = 3, is this affecting also var1? Or did we create a new…
Mannaggia
  • 4,559
  • 12
  • 34
  • 47
0
votes
1 answer

Object scope and lifetime in a for loop

Suppose I have the following code. vector v; \\Cat is a class for (int i = 0; i < 5; i++) { Cat cat1; if (someFunction(i)) { cat1.setName("Whiskers"); v.push_back(whiskers) ; } } My question is, in a for loop,…
Kevin Zakka
  • 445
  • 7
  • 19
0
votes
3 answers

C++ object lifetime and destructors called

I'm trying to understand object lifetime in C++. When I run the code: class Foo { public: Foo(); Foo(const Foo &old); ~Foo(); int x_; }; int nextX = 0; Foo::Foo() { cout << "Foo(): " << (x_ = nextX++) << endl; } Foo::Foo(const…
0
votes
2 answers

Storing member function in a vector and ensuring object still exists when invoked later

I have a class X and I want to pass a member function bar to another class Y, which will store it in a vector and invoke it at some other time. I understand that I need to make sure that X still exists when Y invokes the function. How do I do…
ksl
  • 4,519
  • 11
  • 65
  • 106
0
votes
2 answers

How Can binding lifetime can be longer than object lifetime?

I have read that the name to object binding can have a longer lifetime than the object itself. According to my understanding, when the object is destroyed, then the binding between the name and the object is also gone. Then how can binding lifetime…
Pavan_k_k
  • 309
  • 1
  • 5
  • 11
0
votes
1 answer

Will NSManagedObjectContext live long enough to perform all blocks submitted with -performBlock:?

Consider following code: NSManagedObjectContext *parentContext = ... // global context, exists as long as app runs MyEntity *parentEntity = ... // parentEntity is in parentContext NSManagedObjectContext *context = [[NSManagedObjectContext alloc]…
0
votes
1 answer

Make binding source aware of binding targets death

My WPF app polls some external hardware and shows data changes using binding to INotifyPropertyChanged. It works great so far but I would like it to pause polling when controls that show the data are "dead" and continue when they're on-screen…
Yegor
  • 2,514
  • 2
  • 19
  • 27
0
votes
1 answer

Before_validation :foo, on: :update not working as expected

I'm using Rails 4.0.0. I have the following setup: class Foo < ApplicationController before_validation :foo, on: :create ... private def bar puts 'bar is called' end end This works - in the console, when I create a foo, I…
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
0
votes
2 answers

What happens if we directly return class instance's memory location?

Main function code: PNG* allocate(size_t w, size_t h) { PNG ret(w, h); return &ret; } int main() { PNG * image = allocate(256, 512); delete image; return 0; } Assume that appropriate PNG class is defined. My question is about…
0
votes
3 answers

algorithms that destruct and copy_construct

I am currently building my own toy vector for fun, and I was wondering if there is something like the following in the current or next standard or in Boost? template void destruct(T* begin, T* end) { while (begin != end) { …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
0
votes
0 answers

Javascript object with precise lifetime / run code at end of scope

It is often helpful to have objects with a lifetime tightly related to scope. For example in C++, the destructor for auto objects. In C#, Dispose within a using block. I want to know if there's a way to do this in Javascript. The example that comes…
tenfour
  • 36,141
  • 15
  • 83
  • 142
0
votes
1 answer

Model life managment

i have a Model and class - Factory which creates, load and save this model. I want completely release all references to this Model save and dispose, but i don't know how can i tell all classes who keep references to Model release it. I only see 2…
Towelie
  • 99
  • 2
  • 10
0
votes
2 answers

Error associated with std::unique_ptr

I'm having a problem with std::unique_ptr. I thought I understood them but clearly not. I have the following code: X::X() : m_foo(nullptr), { m_foo = std::unique_ptr(new Foo()); } X::X(Foo* foo) : m_foo(nullptr), { m_foo =…
ksl
  • 4,519
  • 11
  • 65
  • 106
0
votes
3 answers

What is recommended lifetime of Database object in a web application?

I consider using PetaPoco in a conventional web app. Saying conventional I mean handling requests in separate threads from a pool requests are quick: no long polling, streaming, etc but not necessarily ASP.NET, it may be for example Nancy What…
amartynov
  • 4,125
  • 2
  • 31
  • 35