Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do i release respectively dispose the objects which are held from these fuctions below?

VB
 Dim drvInfo As DirectoryInfo = New DirectoryInfo("C:\myFolder")
 Dim filesInfo() As FileInfo = drvInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly)


Try
      For Each file As FileInfo In filesInfo
       If Not file.Attributes = FileAttributes.System Then
       My.Computer.FileSystem.RenameFile(file.FullName, "myFile" + file.Extension)
      Next
Catch
End Try
Posted
Comments
[no name] 7-Aug-12 20:57pm    
Why do you think you need to? If you feel that you must do something, set them = Nothing.
Sergey Alexandrovich Kryukov 7-Aug-12 21:15pm    
Why, indeed?
--SA
[no name] 7-Aug-12 22:22pm    
Because in my code there are 2 functions "For each file as FileInfo in filesInfo" in the same Sub, understand? And the problem is that the second for each fileinfo in bla bla... cannot use the same objects because the garbage collector still hold them and GC needs some time to release them as u know.
[no name] 7-Aug-12 22:55pm    
Why be rude to someone that merely asked for clarification for, what we know now, to be an incomplete question. 2 functions? Where? If there are 2 functions then why do you only show one? And the one that you show is not complete. What possible connection do the 2 functions have that you are using the same objects? Second for each? Where is that? Again you only show one. Why are you worried about the GC? If you expect to get good and complete answers then maybe you should submit good and complete information.
[no name] 7-Aug-12 23:14pm    
I expected that someone who knows the right answer if "possible" to answer my question. I asked how to release it immediately before executing the line after. There is no need to show the full function code i think. When i ask how to release it immediately, then i think i spelt it clearly and if you arent able to answer it then i feel sorry for your vb skillz. Thank you

1 solution

You only need to dispose object of the types implementing System.IDisposable. Please see:
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx[^].

If you work with temporary object, the best way of doing it is using the using statement, which helps to dispose the object you create even if an exception is thrown. Actually, it works exactly like with try-finally block:
http://msdn.microsoft.com/en-us/library/htd05whh.aspx[^].

If you use your objects beyond the scope of a single method, you need to use the chain strategy: dispose everything in the parent's dispose method. …OK, it looks like I'm going to far from the original topic… :-)

[EDIT]

You should not mix up disposal with managed memory allocation. You never need to do anything to release it, the garbage collector does it:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

Actually, this is why .NET (CLR) code is called "managed".

The interface System.IDisposable can be used for any purposes which do not have to be related to release of something. Rather, it is used as a mechanism to guarantee that some "closing" action is always performed. One of the most typical application of this mechanism is the release of unmanaged resources, as in the case of GDI object. This is an interesting example of using disposing not related to releasing of anything:
Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^] (please also see the alternative solution, which is more generalized).

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900