1

I have a code like below where from one method I get a object (just copy from global object without new) and do the operation. I do dispose global object while closing a child form. However, at method level I don't dispose local object. Is this required to dispose/set null to local object as well? What should be the best practice for such scenario.

Please note this object has reference of "Axshockwaveflashplayer" object that's why i am little bit worried about local object as well.

class Test
 {
 Method()
   {
    var obj = GetContainerObject()   //copying object
    var proxy = obj as ExternalIntClass //copying object
    proxy.call("Name", param);
    **//After use should i dispose obj and proxy local objects ?**
   }

 private object GetContainerObject()
   {
    switch(condition)
       case 1 :
         return proxy1;
       case 2:
         return proxy2;
   }

  Dispose()
   {
    proxy1 = null;
    proxy2 = null;
   }

 }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
ManojP
  • 85
  • 1
  • 9
  • What is a "global object"? – Uwe Keim Aug 23 '16 at 11:31
  • 3
    Setting members to `null` is usually _not_ what [`IDisposable`](http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface) objects (should) do. See "[Setting Objects to Null/Nothing after use in .NET](http://stackoverflow.com/questions/2785/setting-objects-to-null-nothing-after-use-in-net)" – Uwe Keim Aug 23 '16 at 11:32
  • 2
    There is no general answer that can be given here other than to check the documentation of the behavior of `GetContainerObject`. If it returns new objects and hand over the ownership of those objects to the caller, then yes, the caller must dispose of the objects (assuming they implement `IDisposable`). If it returns references to existing objects and *does not hand over ownership* then no, the caller should not dispose of those objects, even if they implement `IDisposable`. – Lasse V. Karlsen Aug 23 '16 at 11:34
  • @UweKeim by global object I meant object which has scope for a class and local object is within a method. – ManojP Aug 23 '16 at 11:45
  • @LasseV.Karlsen "GetContainerObject" just returns the reference of proxy1. it doesn't crate a new object (no new operator). However, that object has COM reference so I thought to double sure on this. Is this any harm if i use using block in Method()? – ManojP Aug 23 '16 at 11:48
  • The important part is who owns the objects. – Lasse V. Karlsen Aug 23 '16 at 11:48
  • there is a separate user control class from where my class "Test" get reference of that object after loading flex form. ExternalInterfaceUSerControl -> MyClass Proxy1, Proxy2 -> My Method.. ExternalInterface has dispose pattern implemented even my class"Test" also disposes proxy1, 2.. But not sure about local objects.. – ManojP Aug 23 '16 at 11:52

0 Answers0