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

Implementing a search() method to handle optional case sensitivity

In The Rust Programming Language, there is a chapter which implements a minigrep. Instead of implementing a second search_case_insensitive() method, I wanted to implement a single API which would duplicate code. That attempt went something like…
skittish
  • 121
  • 10
0
votes
1 answer

DbContext not update when multiple requests are sent in a short time

Currently I'm developing a web system for my company using angularjs and web api 2. In the system, we allow users to add account into database by using an api. This is its flow: Call api/user to add user into database. Add user information with…
0
votes
1 answer

MS Access: Way to maintain persistent session across multiple Form Events?

Hopefully this is a simple question, though the solution may not be. In MS Access, is it possible to instantiate a VBA class in the application or database scope? What I want to do is persist a WinHttp instance with the same lifetime as the…
tlum
  • 913
  • 3
  • 13
  • 30
0
votes
2 answers

unique_ptr to a concrete type

#include #include class Base { public: virtual void foo() = 0; }; class Derived : public Base { public: void foo() override { std::cout << "Derived" << std::endl; } }; class Concrete { public: void Bar() {…
0
votes
1 answer

A bug in std::shared_ptr?

What should happen when the following program is executed? #include #include class test; std::shared_ptr a_test_object; struct test { ~test() { std::cout << "destroy test" << std::endl; auto ptr =…
Davis King
  • 4,731
  • 1
  • 25
  • 26
0
votes
1 answer

Resolve elided static lifetime when borrowing from an object pool

This is a simplified version of the issue I am currently facing. trait SuperObject { fn object_name(&self) -> String; } trait Inspect { fn inspect(&self); } impl Inspect for SuperObject { fn inspect(&self) { println!("I am a…
John N.
  • 25
  • 4
0
votes
1 answer

Lifetime of object created without 'new' assigned to pointer

Let's say we have: struct A { int data; }; int main( void ) { { A a; a.data = 4; } cout << "Hello World" << endl; return 0; } I understand that object created without new is stored on the stack and is destructed automatically…
0
votes
1 answer

Caching of anonymous objects in Javascript

This is a nonsensical example used purely for illustration purposes: function a() { return b().bar + foo.bar; } function b() { var foo = { bar: 'baz' }; return foo; } console.log(a()); // Obviously throws a Uncaught ReferenceError: foo…
Anon
  • 109
  • 1
  • 8
0
votes
1 answer

Unity child container HierarchicalLifetimeManager mvc and windows service

I have the following class at my business layer level (simplified code): public class StatusUpdater : IStatusUpdater { private readonly IStatusRepo _statusRepo; public class StatusUpdater(IStatusRepo statusRepo) { _statusRepo =…
SOfanatic
  • 5,523
  • 5
  • 36
  • 57
0
votes
1 answer

MVC Object Instances or Static classes?

I am confused as to when to create object instances or Static Helper classes. For example, if I call a method to update a data model and submit to database, i create an instance of the DataContext. What is the lifetime of that Datacontext and is it…
zsharp
  • 13,656
  • 29
  • 86
  • 152
0
votes
0 answers

Callback object destroys itself while executing when replacing itself with new value

Consider the following situation: struct A { // `unique_function` is like std::function but does not // require its target to be Copyable, and is in turn // itself not Copyable either. unique_function callback = []{}; …
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
0
votes
1 answer

What's the life time of the string object passed into std::runtime_error's ctor?

According to cppreferences, explicit runtime_error( const std::string& what_arg ); won't copy what_arg's content. Can I safely pass a temporary string object into std::runtime_error's ctor? For example: std::string GetATempString(const char* msg) { …
xmllmx
  • 39,765
  • 26
  • 162
  • 323
0
votes
1 answer

Lifetime of dependent classses in C++?

I have a class A that provides methods to construct instances of class B. And B holds a private reference to A and provides a constructor to set this reference. class A { public: B* construct_B (); } class B { private: const A&…
0
votes
0 answers

Outer object owning referencing inner object

I need two objects; one object is a container which owns the second object. struct OuterObject { foo: InnerObject, } impl OuterObject { /// Creates a new instance of OuterObject fn new() -> OuterObject { OuterObject { …
0
votes
1 answer

Remove just last instance of an activity from stack

I have an application with 2 activities A and B, A is the main activity and B is called by A but also can be called from B itself. So my stack will look like A,B0,B1,B2,B3 In certain cases I want to be able to remove only last instance of B, so …
Westside Tony
  • 708
  • 7
  • 20