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

Difference between bcc32 and bcc32c object lifetime

I have a cross platform C++ application build with C++ Builder 10.1 Berlin and have a problem understanding the lifetime handling of objects, in this case strings, wich are declared outside the class. I have created a new forms application and added…
Kerem
  • 429
  • 5
  • 20
3
votes
3 answers

Are singletons automatically persisted between requests in ASP.NET MVC?

I have a lookup table (LUT) of thousands integers that I use on a fair amount of requests to compute stuff based on what was fetched from database. If I simply create a standard singleton to hold the LUT, is it automatically persisted between…
randomguy
  • 12,042
  • 16
  • 71
  • 101
3
votes
2 answers

Using placement new to update a reference member?

Is the following code legal in C++? template class Foo { public: Foo(T& v) : v_(v) {} private: T& v_; }; int a = 10; Foo f(a); void Bar(int& a) { new (&f)Foo(a); } References are not supposed to be bound twice,…
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
3
votes
0 answers

Unity Child Container Lifetime with WCF

We're using Unity to provide dependency injection within the WCF service layer for our current project, and we have followed examples such as the following to write a service host factory, service host, service behaviour and instance…
Justin
  • 564
  • 4
  • 13
3
votes
3 answers

Storage reuse in C++

I have been trying to understand storage reuse in C++. Imagine we have an object a with a non-trivial destructor whose storage is reused with a placement new-expression: struct A { ~A() { std::cout << "~A()" << std::endl; } }; struct B: A…
user3663882
  • 6,957
  • 10
  • 51
  • 92
3
votes
1 answer

Why should I use SendStr (over &str or String) for a message property on an Error struct in Rust?

I found this excellent blog post about error handling in Rust. It defines an error struct as such: struct ProgramError { kind: ProgramErrorKind, message: SendStr } For the message it uses SendStr which is an type alias for…
Christoph
  • 26,519
  • 28
  • 95
  • 133
3
votes
2 answers

How can I remember better when to use which lifetime syntax?

Now that I made myself a little bit familiar with the basic concept of lifetimes in Rust I'm facing a different problem. I'm having a pretty hard time to wrap my head around the lifetime syntax. Is there a general rule when to use which syntax for…
Christoph
  • 26,519
  • 28
  • 95
  • 133
3
votes
2 answers

Can std::initalizer_list cause lifetime issues?

When using std::initializer_list I have experienced some difficulties. It didn't take long to realise that I thought of it more as a container, when in fact it has reference semantics. So my question is, which of the following examples might cause…
Excelcius
  • 1,680
  • 1
  • 14
  • 31
3
votes
1 answer

boost signals - How control lifetime of objects sent to subscribers? Smart pointers?

I am using boost::signals2 under Red Hat Enterprise Linux 5.3. My signal creates an object copy and sends it's pointer to subscribers. This was implemented for thread safety to prevent the worker thread from updating a string property on the object…
Ed of the Mountain
  • 5,219
  • 4
  • 46
  • 54
3
votes
3 answers

Why can a non-const reference parameter be bound to a temporary object?

char f1(); void f2(char&); struct A {}; A f3(); void f4(A&); int main() { f2(f1()); // error C2664. This is as expected. f4(f3()); // OK! Why??? } error C2664: 'void f4(char &)' : cannot convert argument 1 from 'char' to 'char…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
3
votes
2 answers

Lifetime of object created using ApplicationContext.xml in Spring MVC

I am new to spring application... I want to know, When the object created using applicationcontext.xml file will get destroyed?.. i.e., I want to know the life time of the object created by applicationcontext.xml file... I tried a lot in Google…
sree
  • 535
  • 4
  • 12
  • 26
3
votes
2 answers

After an object is destroyed, what happens to subobjects of scalar type?

Consider this code (for different values of renew and cleanse): struct T { int mem; T() { } ~T() { mem = 42; } }; // identity functions, // but breaks any connexion between input and output int &cleanse_ref(int &r) { int *volatile…
2
votes
1 answer

AppDomain only partially respecting InitializeLifetimeService (if at all)

Thanks to Timiz0r, I've solved this issue, but it's multi-step so I'm detailing the full solution here; the original question is below. First, I changed my ILS function to be simply: Public Overrides Function InitializeLifetimeService() As Object …
Kodithic
  • 160
  • 12
2
votes
4 answers

Managing C type lifecycle using boost's shared_ptr?

I have a question similar to How to manage object life time using Boost library smart pointers? but, in my case, the "object" isn't a C++ object at all, but an opaque type returned/passed out from a C API. The type does not have pointer semantics,…
Chris Cleeland
  • 4,760
  • 3
  • 26
  • 28
2
votes
1 answer

Does Spring.NET Support Creating Custom Object Lifetimes?

In the book Dependency Injection in .NET, the author says that Spring.NET doesn't support creation of custom object lifetimes (See last paragraph of section 12.2 [Managing lifetime]). Even though I am new to the framework, I think this may not be…
ehsanullahjan
  • 5,422
  • 4
  • 23
  • 21