object-lifetime

How to inject dependencies per http request (or per http context) with unity 2.0 and asp.net mvc

匆匆过客 提交于 2019-12-11 13:04:57
问题 I've read a lot of info on this, but I can't find anything current (as of 1/2011) as to how exactly this should be done nowadays. I've read a couple of good posts that seem to have the answer, without the details. Older pre-unity-2 approaches mostly created custom lifetime managers. It appears the best way to do it now is to use a child container that is created at the beginning of the request and disposed at the end of the request, using container-managed lifetime. Another person said they

Is casting a temporary with type `int` to a reference safe?

馋奶兔 提交于 2019-12-10 22:25:55
问题 In the following program: int Func() { int a = { 10 }; return a; } int main() { int& r = (int&)(const int&)Func(); r = 5; } r is a reference to a temporary of type int . But temporaries are destroyed immediately unless they are assigned normally to a reference. The assignment above does not seem normal. Is it safe to use r in Standard C++? 回答1: Introduction: the C-style casts are equivalent to (C++17 [expr.cast]): int& r = const_cast<int&>( static_cast<const int&>(Func()) ); In the

Multiple constructor with Structuremap changing the scope?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 16:53:01
问题 To illustrate the problem, here is a simplified version of my setup. I have a factory like this one : public interface IFactory{ } public class Factory : IFactory { public Factory() { Console.WriteLine("parameterless"); } //public Factory(int i) //{ // Console.WriteLine("with parameter : {0}", i); //} } the program to test this is a consoleApp. Enough to prove my point. static void Main(string[] args) { Init(); var factory1 = ObjectFactory.GetInstance<IFactory>(); var factory2 = ObjectFactory

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

跟風遠走 提交于 2019-12-10 13:12:20
问题 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 are being called.) Example #include <string> struct Base; struct Member { Member(Base *b); ~Member(); Base *b_; }; struct Base { virtual void f() {} virtual ~Base() {} }; struct Derived : Base { Derived() : m(this) {} virtual ~Derived() {} virtual

Android - is onDestroy supposed to destroy the activity, its variables and free up memory

江枫思渺然 提交于 2019-12-09 09:44:37
问题 I have a bug in my code that made me think I don't fully understand the Android Lifecycle. Yes, I have read all the docs and looked at the diagrams, but they seem to talk only about when to save data, when the activity may loose focus or get killed. However, my question is if I don't need to save state, what happens to the variables & their stored values? I expected them to be destroyed to, but a bug in my code seems to indicate otherwise. In my case here is what happened. I have an activity

Private field captured in anonymous delegate

柔情痞子 提交于 2019-12-09 05:19:24
问题 class A { public event EventHandler AEvent; } class B { private A _foo; private int _bar; public void AttachToAEvent() { _foo.AEvent += delegate() { ... UseBar(_bar); ... } } } Since delegate captures variable this._bar , does it implicitly hold to the instance of B ? Will instance of B be referenced through the event handler and captured variable by an instance of A ? Would it be different if _bar was a local variable of the AttachToAEvent method? Since in my case an instance of A lives far

What's the point of temporary bound to a member lifetime statement in C++ Standard?

家住魔仙堡 提交于 2019-12-08 17:09:41
问题 In this question user Happy Mittal quotes section 12.2.5 of C++03 Standard: A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits . How can that be useful anyway? I mean once the constructor exits the temporary gets destroyed, but the reference remains bound - now to an already destroyed object. What's the point of so carefully specifying the temporary lifetime if there's still a dangling reference for the whole lifetime of

does passing a method of one object to another object keep the first object alive?

心已入冬 提交于 2019-12-07 06:27:12
问题 Suppose I have three objects: 'a', 'b' and 'c'. Object 'a' and 'c' are long-lived, statically referenced service singletons. Object 'b' is short-lived, i.e. no static references keep it alive. Now suppose object 'a' creates an instance of object 'b' in the scope of one of its methods, e.g. B b = new B(); Further suppose that the class B looks something like this: public B () { C.ActionList.Add ( SomeMethod ); } void SomeMethod () { ... } Now, how long does object 'b' live? My presumption is

Lifetime of Qt Objects

馋奶兔 提交于 2019-12-05 20:00:34
问题 What are the lifetimes of Qt Objects? Such as: QTcpSocket *socket=new QTcpSocket(); When socket will be destroyed? Should I use delete socket; Is there any difference with: QTcpSocket socket; I couldn't find deep infromation about this, any comment or link is welcomed. 回答1: Qt uses parent-child relationships to manage memory. If you provide the QTcpSocket object with a parent when you create it, the parent will take care of cleaning it up. The parent can be, for example, the GUI window that

Dependency injection and life time of IDisposable objects

百般思念 提交于 2019-12-05 16:16:18
I am trying to develop a library using dependency injection approach (with Ninject) and I am having some kind of confusion likely because of my incorrect design. In summary, my design approach is A parent object has a common object. A parent object uses some variable number of child objects. All child objects should use the very same common object instance with their parent object Here is a simple model of my problem domain. interface IParent : IDisposable { void Operation(); } interface ICommon : IDisposable { void DoCommonThing(); } interface IChild1 { void DoSomething(); } interface IChild2