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

Is it undefined behavior to run a member function in a separate thread, in parallel to the type's constructor?

This is a scenario you shouldn't ever do, but https://timsong-cpp.github.io/cppwp/class.cdtor#4 states: Member functions, including virtual functions ([class.virtual]), can be called during construction or destruction ([class.base.init]). Does…
6
votes
4 answers

Why variable that declared in a using statement treated as readonly?

why variable (myform) in using block treated as read-only and the compiler raise an error when I try to pass it as a reference to a function. sample code: using (Form myform = new Form) { myfunc(ref myform); } passing using variable to a…
Saleh
  • 2,982
  • 5
  • 34
  • 59
6
votes
2 answers

Lifetime extension of temporary by non-const reference using const-cast

This is something that came up recently and which I feel shouldn't work as it apparently does: #include #include int main() { std::shared_ptr& ptr = const_cast&>( static_cast
Steelbadger
  • 135
  • 6
6
votes
2 answers

Pointer to deallocated variable changes address

This code: #include using namespace std; int* fun() { int a = 5; int* pointerA = &a; cout << pointerA << endl; return pointerA; } int main() { int* p = fun(); cout << p << endl; return 0; } Prints the…
6
votes
3 answers

ElasticSearch.NET connection/client management lifecycle

When I set up a connection to my ElasticSearch cluster using ElasticSearch.NET, I am using a code block like the following: var uris = settingsProvider.ElasticSearchUri.Split(';').Select(x => new Uri(x)); var sniffingConnectionPool = new…
davidadsit
  • 163
  • 1
  • 6
6
votes
4 answers

Should this C++ temporary binding to reference member be illegal?

My question (which will follow after this, sorry about the long intro, the question is down there in bold) is originally inspired by Item 23 in Herb Sutters Exceptional C++ where we find something like this: ... int main() { …
martin
  • 75
  • 7
6
votes
2 answers

How should delegator object lifespans be extended for the duration of a delegate callback?

I'm going to distill a very common situation to a general form. Say I'm building some library object that goes off, does some async work, then calls back to a delegate method when it's done. Now, also say for some arbitrary reason that I can't use…
Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
6
votes
3 answers

The state of an object between a call to ~Derived() and ~Base()

Question What does the C++ standard guarantee about the state of an object in the time after a derived class's destructor executes, but before the base class's destructor executes ? (This is the time when the derived class's subobjects' destructors…
Nate Kohl
  • 35,264
  • 10
  • 43
  • 55
5
votes
2 answers

std::start_lifetime_as and UB in C++23 multithreaded application

Assuming X and Y are suitable types for such usage, is it UB to use std::start_lifetime_as on an area of memory in one thread as one type and use std::start_lifetime_as on the exact same memory in another thread? Does the standard say…
markt1964
  • 2,638
  • 2
  • 22
  • 54
5
votes
1 answer

Need help understanding how luabind instantiates classes

Let's say I have a class like this: class A { public: A(){} ~A(){} }; And expose it to Lua via Luabind like this: module(luaState) [ class_("Foo") .def(constructor<>()) ]; And finally instantiate it in a script like…
Erius
  • 1,021
  • 8
  • 21
5
votes
1 answer

Is it safe to store string literals pointers?

According to the standard: 5.13.5 String literals [lex.string] 16 Evaluating a string-literal results in a string literal object with static storage duration, initialized from the given characters as specified above. Whether all string literals…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
5
votes
2 answers

What is the best object lifetime strategy for Redis in web application

I will plan to use Redis (ServiceStack) as whole database for web application. I can insert 76000 records in 7.4 seconds. But using single connection (RedisClient object-life-time is Application), I used Set generic method not Store (huge…
5
votes
0 answers

When a union object is copied, is a member subobject created?

When another member of a union is accessed, the C++ standard used to be silent on what happens, but that was fixed to explain that member access to a union object was allowed for the purpose of assigning to that yet no existent object, which would…
curiousguy
  • 8,038
  • 2
  • 40
  • 58
5
votes
3 answers

Is this a proper use of a temporary std::string?

std::string getMyString() { return ; } ... HANDLE something = OpenSomething(getMyString().c_str(), ...); I've read Guaranteed lifetime of temporary in C++ and I believe that the temporary string will live until the assignment has…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
5
votes
2 answers

Is taking the address of a member of an uninitialized object well defined?

Consider the following example. When bar is constructed, it gives it's base type (foo) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struct foo { foo(int * p_x) : x(p_x) {} int *…