How do you dispose of an IDisposable in Managed C++?

后端 未结 1 1147
天涯浪人
天涯浪人 2021-01-07 18:47

I\'m trying to Dispose of an IDisposable object(FileStream^ fs) in managed C++ (.NET 2.0) and am getting the error

Dispose\' : is not a member of \'Syste

相关标签:
1条回答
  • 2021-01-07 19:33

    The correct pattern is to just delete the object:

    delete fs;
    

    This will be translated into a call to Dispose().

    See this post for some of the details of what is going on under the hood. The advantage of this idiom is that it allows you to write:

    {
      FileStream fs(...)
      ...
    }
    

    And have the Dispose method called correctly ... equivalent to a using block in C#. The file stream object is still allocated on the managed heap.

    0 讨论(0)
提交回复
热议问题