Software Archive
Read-only legacy content
17061 讨论

Does C# PXCMSenseManager.Dispose() call PXCMSenseManager.Close()?

Niels_Dekker
初学者
770 次查看

Is the PXCMSenseManager.Close() call in the following code necessary, or is it redundant?

  using ( PXCMSenseManager myManager = session.CreateSenseManager() )
  {
      // Usually some more code here...
      // ...

      myManager.Close();
  }

As you probably know, C#'s 'using' statement does internally call Dispose() when the declared object (myManager) goes out of scope. Will this Dispose() call indirectly call PXCMSenseManager.Close()? Or will it do something equivalent?

Kind regards, Niels
--
Niels Dekker
Scientific programmer at LKEB
Leiden University Medical Center

0 项奖励
4 回复数
AndreCarlucci
初学者
770 次查看

Hi Niels,

Excellent question. Usually with the .net framework that's the case, but I'm not sure looking at the source code of the libpxcclr.cs.dll.

When we create a PXCMSenseManager, an object called handlerDIR (internal class) is also created. It implements IDisposable.

public pxcmStatus Init(Handler handler) {
        if (handler == null) {
            return PXCMSenseManager_Init(instance, IntPtr.Zero);
        }
        if (handlerDIR != null) {
            handlerDIR.Dispose();
        }
        handlerDIR = new HandlerDIR(this, handler);
        return PXCMSenseManager_Init(instance, handlerDIR.dirUnmanaged);
}

// Calling Close will dispose the handleDIR

public void Close() {
        PXCMSenseManager_Close(instance);
        if (handlerDIR == null) {
            return;
        }
        handlerDIR.Dispose();
}
[DllImport("libpxccpp2c")]
internal static extern void PXCMSenseManager_Close(IntPtr putil);

---------------

PXCMSenseManager inheriteds from PXCMBase (IDisposable). This is the Dispose method:

public virtual void Dispose() {
        int num;
        lock (this) {
            num = refcount;
            if (refcount > 0) {
                --refcount;
            }
        }
        if (num > 0 && instance != IntPtr.Zero) {
            PXCMBase_Release(instance);
        }
        else {
            orig = this;
        }
}
[DllImport("libpxccpp2c")]
internal static extern void PXCMBase_Release(IntPtr pbase);

Instance is a pointer to the unmanaged object.

 

I can't tell you for sure because I don't have access to the  unmanaged code, but It seems that calling Close disposes this handlerDIR and calling Dispose don't, so it sounds like a good idea to call them both.

Maybe someone from Intel can help us with that :)

 

 

 

 

 

 

0 项奖励
Niels_Dekker
初学者
770 次查看

Thank you for your very informative reply, Andre Carlucci! I did not realize before that the C# source code of those RealSense SDK classes is already part of my installation, at "C:\Program Files (x86)\Intel\RSSDK\framework\common\pxcclr.cs\src". Now I see, the implementation of PXCMSenseManager is partially at pxcclr.cs\src\pxcmsensemanager.cs and partially at pxcclr.cs\src\native\pxcmsensemanager.cs  But it seems like PXCMSenseManager does not have a Dispose() override.

So do you think PXCMSenseManager should have a Dispose() override, in order to dispose its handlerDIR data member?

If you think this is a bug, can you please report it to Intel? (Or otherwise, can you tell me where to report the bug?)

 

Note that calling both myManager.Close() and myManager.Dispose(), triggers a Code Analysis warning from Visual Studio:

warning CA2202: Microsoft.Usage : Object 'myManager' can be disposed more than once in method '...'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.

This warning even appears when the Dispose() call is done implicitly, by using 'using'.

 

Kind regards, Niels

0 项奖励
AndreCarlucci
初学者
770 次查看

Oh man, I didn't realize the code was there in the installation all the time.

I went up to the pxclr.cs directory and stopped there, since my eyes didn't catch that was the same name without the "lib" part.

That source code I posted above came from my decompiled version that is on github: https://github.com/SharpSenses

I'll update github (and SharpSenses) to use the original code as soon as I have the time.

It's funny that I asked in this forum a lot about this source and nobody could tell me where it was or even if it was available. So thanks :)

---

About the error, you can easily override the Dispose and call Close and the parent Dispose now that you have the source-code. Go ahead and compile the dll to be AnyCpu too and be happy :)

I'm not sure if this is really a bug or if it's intended for some reason though. I'll ask around :)

Cheers!

0 项奖励
Niels_Dekker
初学者
770 次查看

Thanks again for your feedback, André!

FYI, I just posted a similar C++ question: Does Release() call Close(), for a C++ pointer to a PXCSenseManager?

Kind regards, Niels

 

0 项奖励
回复