CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: dispose issue

  1. #1
    Join Date
    Jul 2010
    Location
    Watch Window
    Posts
    87

    Question dispose issue

    can anyone tell me ???what is the difference between dispose and destructor.let me know please.

  2. #2
    Join Date
    Dec 2008
    Posts
    144

  3. #3
    Join Date
    Jul 2010
    Location
    Watch Window
    Posts
    87

    Question Re: dispose issue

    can someone tell me ???why the following code is not compiling ?.i am getting error .when tried to complie.
    Code:
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace Example13
    {
    classBoxme
    {
    int itakeObj(Object x)
    {
    Console.WriteLine(x);
    }
    object iSpitObs()
    {
    
    int v;
    v=5;
    BoxMe Up=newBoxme();
    up.itakeObjs(virtual);
    int boxBox=(int)up.isitObjs();
    }
    }
    }
    
    Attached Images Attached Images

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: dispose issue

    That code is nonsensical. virtual is a reserved keyword.

    Compiler errors are the most simple errors to solve. Every programmer starts out this way, eventually the compiler stops nagging and you can start to encounter real problems. I suggest that you take the time to read the error message and do a little research to figure out why it is happening.

  5. #5
    Join Date
    Jul 2010
    Posts
    82

    Re: dispose issue

    Yep, virtual cant be passed in there, also, the function does return nothing with Object X. What are you trying to do here??

  6. #6
    Join Date
    Jul 2010
    Posts
    82

    Re: dispose issue

    Destructors are methods or functions. Its used to exit from the object execution gracefully. So, if you have used recsources, its a good practice to dispose them off after you're done with the object. Dispose as such is used to dispose objects by the .NET runtime. You call Dispose() of resources which implements IDisposable , that will release back the memory it once occupied. GC does that periodically, but its good, if your object has consumed a lot of resources, then release it once the object is done with what its doing.

  7. #7
    Join Date
    Jul 2010
    Posts
    82

    Re: dispose issue

    Note that GC is called by runtime periodically based on a no. of factors, so there is no guarantee the resources would be released at any point of time. System.GC() to make it collect, or use the destructor to free all the resources used.

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: dispose issue

    Quote Originally Posted by CuteAssistant View Post
    Destructors are methods or functions. Its used to exit from the object execution gracefully. So, if you have used recsources, its a good practice to dispose them off after you're done with the object. Dispose as such is used to dispose objects by the .NET runtime. You call Dispose() of resources which implements IDisposable , that will release back the memory it once occupied. GC does that periodically, but its good, if your object has consumed a lot of resources, then release it once the object is done with what its doing.
    Well, that's not really true. IDisposable has nothing to do with releasing the memory consumed by .NET objects, it is all about releasing native resources that will not be cleaned up automatically by the runtime (i.e., file handles, DIBs, DC's, etc.)

    The whole point is that it gives you deterministic deallocation of resources. The finalizer of .NET objects *will* in fact release these resources, it is simply non-deterministic.

    Also, please, please, do not call GC.Collect( ) unless you have *proven* that it is necessary for your situation, which it rarely is.
    Last edited by BigEd781; July 20th, 2010 at 03:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured