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
4 answers

Constrain the lifetime of a data member to one method

I have encountered a slightly unusual problem. Consider the following code: class parser { lexer lex; public: node_ptr parse(const std::string& expression) { lex.init(expression.begin(), expression.end()); // ... …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
2
votes
1 answer

Undebuggable non-deterministic heisenbug in single-threaded C++ function call

I'm at the end of my rope here: I have a single-threaded C++ program. Here is some empirical data and background information, I tried to highlight the most important keywords; The entire section I'm talking about does not have any syscalls, other…
bitmask
  • 32,434
  • 14
  • 99
  • 159
2
votes
2 answers

Need an explanation on Lifetime of Objects Created in a Thread

I have a DevExpress GridControl bound to a BindingList. I tried to modify the BindingList from a thread and this threw an exception, I googled it and found the following explanation: This issue is not connected with the XtraGrid directly.…
TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101
1
vote
2 answers

SL custom control: When unregister event to prevent memory leak?

I'm creating some custom wp7 silverlight custom controls which register some events on template children in OnApplyTemplate(). I think I must unregister these to prevent a memory leak. But when? I'll tried the unloaded event - this works but I have…
Ralf Ehlert
  • 400
  • 2
  • 10
1
vote
2 answers

Chaining calls to temporaries in C++

I have a class that does a transformation on a string, like so class transer{ transer * parent; protected: virtual string inner(const string & s) = 0; public: string trans(const string & s) { if (parent) return…
zounds
  • 773
  • 8
  • 17
1
vote
1 answer

Does lifetime extension also construct an object on the stack?

Given a function returning an object of class type T T GetT() { T t; ... return t; } To me, the only difference between lifetime extension const T& t1 = GetT(); and normal construct T t2 = GetT(); // copy elision is that t1 is a const…
Yang Zhao
  • 315
  • 1
  • 5
1
vote
1 answer

decltype(auto) return type and lifetime issues

I am just looking at some code with the following rough outline. #include template decltype(auto) do_something(T&& object, F f) { return f(std::forward(object)); } struct object { int x; int& ref()…
Blair Davidson
  • 901
  • 12
  • 35
1
vote
0 answers

Classes with runtime parameters built by custom factories: how to honor container lifetime using .NET Core DI?

Given some class that relies on a constructor parameter that can only be obtained at runtime, how do I ensure instances of that class have their lifetimes still be controlled by the Microsoft DI container if I'm using a custom factory to bypass the…
julealgon
  • 7,072
  • 3
  • 32
  • 77
1
vote
1 answer

Make rvalues persist as pointers in a recursive class

I have a simple linked list node class which I can use to create a list 1->2->3 like so: struct ListNode { int data_; ListNode *child_; ListNode(int data, ListNode *child) : data_{data}, child_{child} {} }; ListNode node3(3,…
1
vote
0 answers

How can I pass a value and a reference to it into a struct

I've been recently learning Rust while trying to make a Synthesia-like app (similar to a piano roll). While separating some of the code into its own module, I got into trouble trying to write a "constructor" method that can hold onto some data. Here…
1
vote
3 answers

Is it really possible to separate storage allocation from object initialization?

From [basic.life/1]: The lifetime of an object or reference is a runtime property of the object or reference. A variable is said to have vacuous initialization if it is default-initialized and, if it is of class type or a (possibly…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
1
vote
1 answer

Does storage reuse really require object destruction?

According to [basic.life/1] (bold emphasis mine): The lifetime of an object of type T begins when: storage with the proper alignment and size for type T is obtained, and its initialization (if any) is complete (including vacuous initialization)…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
1
vote
1 answer

Load viewstate on pageload, save on Page Unload (from baseclass) - c# Asp.net

Pardon me for asking a mundane newbie question but I seem to be stuck in a class life-cycle limbo. So I have my page public partial class DefaultPage : BasePage { ... } And the BasePage like this: public class BasePage : System.Web.UI.Page {…
LocustHorde
  • 6,361
  • 16
  • 65
  • 94
1
vote
1 answer

Creating/destroying an object in a static memory section

Is the static memory section alignas(alignof(T)) char bytes[sizeof(T)] suitable to hold an instance of T during its lifetime by calling std::construct_at(bytes, ...) / std::destroy_at(bytes)? My instincts say, yeah, the alignment and size…
plasmacel
  • 8,183
  • 7
  • 53
  • 101
1
vote
0 answers

Is it important to use InitializeLifetimeService for windows service members

Does I have to set the InitializeLifetimeService on the members that should be running as long as my application or service is running? by example of my own experience Because I build some services with a timers but the timers stop working after a…
guyl
  • 2,158
  • 4
  • 32
  • 58