locked
Do I need to learn how to release unmanaged resources RRS feed

  • Question

  • User1922541779 posted

    Hi     my assumption is that by now (2019) the asp.net library contains every thing I need. Just give me on case where I need to  write code to release unmanaged resource.

    I read articles about how to write code by implementing IDispose / Finalize to manage files!! isn't that ridiculous? I already have many classes to deal with files: File FileStream File Uplaod ....Or am I not understanding the whole idea about unmanaged resources.Please help to clarify this to me in simple way

    Wednesday, March 13, 2019 4:41 PM

Answers

  • User475983607 posted

    thanks again: Do you know any situation where I have to write code like this. I mean  Dllimport something ? So why ever need to learn this ?

    Consuming COM is the most common, at least, in my experience.  The link in my first post has this type of information.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 13, 2019 6:50 PM

All replies

  • User475983607 posted

    Hi     my assumption is that by now (2019) the asp.net library contains every thing I need. Just give me on case where I need to  write code to release unmanaged resource.

    I think you misunderstand or maybe I do not understand your question.  Managed code is .NET framework code managed by the runtime. Unmanaged code is not managed by the runtime and requires the programmer to release resource, whatever those resource might be, when the object is disposed.

    You need to read the reference docs that come with the unmanaged code to know what to release.

    https://docs.microsoft.com/en-us/dotnet/framework/interop/

    Wednesday, March 13, 2019 5:10 PM
  • User1922541779 posted

    Thanks mgebhard    Just after writing this post I searched for an example and found this example : My question what exactly this code is trying to do? do I need it? 

    .https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.safehandles.safefilehandle?view=netframework-4.7.2

    Wednesday, March 13, 2019 5:38 PM
  • User475983607 posted

    kobosh

    Thanks mgebhard    Just after writing this post I searched for an example and found this example : My question what exactly this code is trying to do? do I need it? 

    .https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.safehandles.safefilehandle?view=netframework-4.7.2

    The linked reference document clearly explains what to do...

     Important

    This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

    Wednesday, March 13, 2019 5:44 PM
  • User1922541779 posted

    Thanks again : I can create/open  file using  filestream  because filestream is doing what this code is doing for me.....So why do I need to use this code?

    Wednesday, March 13, 2019 6:06 PM
  • User475983607 posted

    Thanks again : I can create/open  file using  filestream  because filestream is doing what this code is doing for me.....So why do I need to use this code?

    I'm not sure why you are using SafeFileHandle class or feel you need to.  

    Wednesday, March 13, 2019 6:29 PM
  • User1922541779 posted

    thanks again: Do you know any situation where I have to write code like this. I mean  Dllimport something ? So why ever need to learn this ?

    Wednesday, March 13, 2019 6:39 PM
  • User475983607 posted

    thanks again: Do you know any situation where I have to write code like this. I mean  Dllimport something ? So why ever need to learn this ?

    Consuming COM is the most common, at least, in my experience.  The link in my first post has this type of information.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 13, 2019 6:50 PM