Click here to Skip to main content
15,893,381 members
Home / Discussions / C#
   

C#

 
AnswerRe: Automatically cleanup of "managed resources" when object dies Pin
AhsanS14-Aug-08 22:28
AhsanS14-Aug-08 22:28 
GeneralRe: Automatically cleanup of "managed resources" when object dies Pin
invictus315-Aug-08 0:39
invictus315-Aug-08 0:39 
GeneralRe: Automatically cleanup of "managed resources" when object dies Pin
AhsanS15-Aug-08 1:02
AhsanS15-Aug-08 1:02 
GeneralRe: Automatically cleanup of "managed resources" when object dies Pin
invictus315-Aug-08 1:15
invictus315-Aug-08 1:15 
GeneralRe: Automatically cleanup of "managed resources" when object dies Pin
AhsanS15-Aug-08 1:43
AhsanS15-Aug-08 1:43 
GeneralRe: Automatically cleanup of "managed resources" when object dies Pin
invictus316-Aug-08 3:10
invictus316-Aug-08 3:10 
GeneralRe: Automatically cleanup of "managed resources" when object dies Pin
Scott Dorman16-Aug-08 5:16
professionalScott Dorman16-Aug-08 5:16 
GeneralRe: Automatically cleanup of "managed resources" when object dies Pin
invictus316-Aug-08 6:20
invictus316-Aug-08 6:20 
Thanks for the excellent information. So to sum it up, when will the Dispose() method be called? at the end of using() {} and whenever I call it explicitly, and when the GC decides that "for this object the Dispose() method needs to be called" ? Or will it happen for every object that has the IDisposable interface implemented?

By the way, is a filehandler even needed to be cleaned up in the Dispose() or will the StreamWriter Disposer take care of it automatically?

What I mean with the latest is as follows: I wrote a small tcp server app once. In this app I had a main thread that just waited for connections. When the connection was established, a new object was created and left "on its own" for processing. I will try to illustrate by some pseudo-code:

class Handler
{
  private Socket sock = null;
  private Thread t = null;

  public Handler(Socket new_socket)
  {
     sock = new_socket;
     t = new Thread(DoSomeStuff());
  }

  public void DoSomeStuff()
  {
    //...do some processing here
  }
}

//... In the main thread:

server.Listen(10);

while(true)
{
  Socket new_client = server.Accept();
  new Handler(new_client);
}


As you can see, each handler live on its own and no reference is needed in the main thread after the object is created since the object/thread will do ALL the processing on the established connection. Will the Handler() object in this case be marked as "to be collected by the GC" since I don't have any references to the object? Is this just plain BAD code to solve the problem in terms of memory management?
GeneralRe: Automatically cleanup of "managed resources" when object dies Pin
Scott Dorman16-Aug-08 7:04
professionalScott Dorman16-Aug-08 7:04 
AnswerRe: Automatically cleanup of "managed resources" when object dies Pin
invictus320-Aug-08 11:17
invictus320-Aug-08 11:17 
QuestionGoogle Contacts API fails with 403 Forbidden Always Pin
Vasudevan Deepak Kumar14-Aug-08 8:32
Vasudevan Deepak Kumar14-Aug-08 8:32 
QuestionStatic objects and terminal server Pin
fredsparkle14-Aug-08 6:45
fredsparkle14-Aug-08 6:45 
AnswerRe: Static objects and terminal server Pin
Manas Bhardwaj14-Aug-08 6:47
professionalManas Bhardwaj14-Aug-08 6:47 
GeneralRe: Static objects and terminal server Pin
fredsparkle14-Aug-08 6:55
fredsparkle14-Aug-08 6:55 
AnswerRe: Static objects and terminal server Pin
S. Senthil Kumar14-Aug-08 21:03
S. Senthil Kumar14-Aug-08 21:03 
QuestionMoving or deleting files failing Pin
Ekjon14-Aug-08 6:37
Ekjon14-Aug-08 6:37 
AnswerRe: Moving or deleting files failing Pin
Manas Bhardwaj14-Aug-08 6:40
professionalManas Bhardwaj14-Aug-08 6:40 
AnswerRe: Moving or deleting files failing Pin
Le centriste14-Aug-08 6:46
Le centriste14-Aug-08 6:46 
GeneralRe: Moving or deleting files failing Pin
Ekjon14-Aug-08 7:31
Ekjon14-Aug-08 7:31 
QuestionPopulating a combobox from a collection Pin
Jonso14-Aug-08 5:47
Jonso14-Aug-08 5:47 
AnswerRe: Populating a combobox from a collection Pin
Giorgi Dalakishvili14-Aug-08 5:58
mentorGiorgi Dalakishvili14-Aug-08 5:58 
AnswerRe: Populating a combobox from a collection Pin
acqy14-Aug-08 21:03
acqy14-Aug-08 21:03 
QuestionSave sorted ListBox Items?? Pin
Imran Adam14-Aug-08 5:35
Imran Adam14-Aug-08 5:35 
AnswerRe: Save sorted ListBox Items?? Pin
Imran Adam14-Aug-08 5:37
Imran Adam14-Aug-08 5:37 
GeneralRe: Save sorted ListBox Items?? Pin
Manas Bhardwaj14-Aug-08 6:45
professionalManas Bhardwaj14-Aug-08 6:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.