Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted
Anyone got any thoughts on setting their object references to nothing? I've found conflicting information on the subject: some say that the garbage collector is smart enough to clean up our mess and others say to use the old Set objXXX = Nothing like VB6. So far I've been safely assuming the former is correct, but just wondered if anyone has found different?
--tim
  • *Gurus*
Posted
The short answer is yes, it's clever enough. As soon as the variable goes out of scope it is essentially set equal to nothing anyway, as it was in COM and VB6. The only possible advantage is that the GC *might* release it slightly sooner.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • *Experts*
Posted

I've been wondering, when using GDI+ objects such as brushes

and pens, do you have to call the Dispose method of each one

when their use is finished to free up memory, or does the GC

dispose them automatically when they go out of scope?

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted

Some objects you MUST call Dispose on yourself, to free memory. Things like DeviceContexts come to mind though I haven't used them in .NET. I know Graphics objects that you create must be disposed (not the ones passed in as an argument to a Paint event). I suspect that the Graphics object is creating a DeviceContext behind the scenes. If you don't call Dispose it doesn't get released til the Garbage Collector runs (and maybe not even then).

 

There are other things to be aware of when using Dispose, especially if you create your own object. Read through the help on it to get more info.

 

-n

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...