Ivan F Moala
MrExcel MVP
- Joined
- Feb 10, 2002
- Messages
- 4,209
As far as setting objects equal to Nothing when you're code routine is nearing completion, I used to do this as well as I thought it "cleaned up" my code. In most languages (that I've seen) this is a good thing. Although in VBA it is not needed, as the objects are destroyed when the routine completes and loses focus anyway. So I stopped writing it out as it was being done in the next operation anyway (exiting the routine).
Take care all!
I disagree with all those who purport to that. i.e. setting objects = nothing is NOT required. I beleive it is good pratice to Release/Destroy objects on closing, in order of creation FCFD (First created First Destroyed) especially when automating COM objects eg DAO and ADO. Yes COM objects have calls to AddRef and Release on objects (it actually keeps count of refs set), this is a part of the COM contract. Even if the the documentation says it is destroyed upon closing I would still do this.
The cost to add just a few lines of code is nothing, as opposed to the cost of a left over instance(s) in the ROT or ghost threads with there subsequent drain on resources (memory leaks and process time)
The same is true of API programing where you must use the corresponding Destroy/Release/Delete API
eg
LoadBitmapA > DeleteObject
CreateBitmap > DeleteObject
CreateSolidBrush > DeleteObject
CreateCursor > DestroyCursor
CreateCompatibleDC > DeleteDC
GetDC > ReleaseDC
CreateFontA > DeleteObject
CreateIcon > DestroyIcon
ExtractIconA > DestroyIcon
CreateMetaFileA > CloseMetaFile
CopyMetaFileA > DeleteMetaFile