locked
Exported class implements ICloseable

    Question

  • Just today I noticed some weird behaviour in our WinRT component: when passing an object from C++ to JavaScript, I found that it has a close() method, but there's no such method defined in the class. In the WinMD file it is described as an implementation of `void ICloseable::Close()`.

    Now I gather it has something to do with language projection but I wonder what the exact implications of this are:

    Can I still add my own `void Close()` method? Will I need to do something special in there?
    (Edit: I just did that and now the former Close method is exported as "$MD$81()", so probably no problem there)

    What happens if I call this method from within JavaScript? Is it safe? Does it call `IUnknown::Release()` (confusing the reference count) or even just call the destructor straight-away? Or does it somehow recognize that it was called from user code instead of JS garbage collection and just ignores the former? 


    Wednesday, March 27, 2013 1:54 PM

Answers

  • As far I know, every WinRT interfaces are COM based and derived from IUnknown with reference count based memory management. A ref class is allocated in dynamic memory and the memory it points to is automatically destroyed when the last handle-to-object goes out of scope or is explicitly set to nullptr.

    Windows Runtime types are unmanaged. For all WinRT component there is a Close method which aborts any pending operations and releases all unmanaged resources associated with that object. In JavaScript you need to call the Close method explicitly. In C# and VB.NET, the Close method is exposed as the Dispose() method. For C++, the Close method will be called implicitly when using the delete keyword on the object. In all cases, I think, finalizers are likely to make the IUnknown::Release() call.


    • Edited by Mokarrom Hossain Wednesday, March 27, 2013 4:49 PM
    • Proposed as answer by Jesse Jiang Friday, March 29, 2013 2:20 AM
    • Marked as answer by Jesse Jiang Monday, April 01, 2013 1:38 AM
    Wednesday, March 27, 2013 4:45 PM

All replies

  • As far I know, every WinRT interfaces are COM based and derived from IUnknown with reference count based memory management. A ref class is allocated in dynamic memory and the memory it points to is automatically destroyed when the last handle-to-object goes out of scope or is explicitly set to nullptr.

    Windows Runtime types are unmanaged. For all WinRT component there is a Close method which aborts any pending operations and releases all unmanaged resources associated with that object. In JavaScript you need to call the Close method explicitly. In C# and VB.NET, the Close method is exposed as the Dispose() method. For C++, the Close method will be called implicitly when using the delete keyword on the object. In all cases, I think, finalizers are likely to make the IUnknown::Release() call.


    • Edited by Mokarrom Hossain Wednesday, March 27, 2013 4:49 PM
    • Proposed as answer by Jesse Jiang Friday, March 29, 2013 2:20 AM
    • Marked as answer by Jesse Jiang Monday, April 01, 2013 1:38 AM
    Wednesday, March 27, 2013 4:45 PM
  • Thank you for your reply. Up to now, we never had to invoke this auto-generated Close method from JavaScript, so I'm not sure we're getting the full picture here. The fact that the mangled "$MD$81()" function gets exported after implementing a custom method also called "Close" makes me think that the JavaScript engine will call it automatically.

    So I'm wondering what the implications are if I were to invoke it manually and whether there are circumstances in which I would like to do this.

    Wednesday, March 27, 2013 4:55 PM