Garbage Collection - Pointer out of scope, or not?

From: Mark Prenter (sorry_at_toomuchspam.com)
Date: 07/23/04


Date: Fri, 23 Jul 2004 11:16:05 -0700

Hi, I've got a question about .NET and pointer scope. Basically I'm trying
to find out how smart .NET is with it's garbage collection and realizing
when a variable is no longer used.

For the sake of this question, regardless of poor programming practices,
lets say I have a custom object which has a public variable called "fs"
which will be a pointer to a FileSteam (FileStream*). And then lets say
that in one of my functions, I create a different FileStream, call it "fs2",
but point "fs" to "fs2", so that both FileStream pointers are pointing at
the same FileStream object.

I would assume, that at the end of the function, "fs2" should go out of
scope. Is .NET smart enough to realize that "fs" is still pointing to the
object and not garbage collect it? Or will it eventually be garbage
collected, leaving "fs" pointing to nothing?

/\/\ark



Relevant Pages

  • Re: Tutorial for CComPtr and CComQIPtr
    ... Apologies for confusing the topic with the mention of "garbage ... pointer" and the "Smart pointer". ... The template class Ptrimplements a garbage collecting pointer to any ... class derived from RefCount. ...
    (microsoft.public.vc.atl)
  • Re: portable finalizer
    ... finalization in SBCL, but I want to make my code portable. ... is a pointer to a C structure. ... so there is a destroy method: ... object destroyed when it is garbage collected. ...
    (comp.lang.lisp)
  • Re: Arithmetic on function address
    ... The Standard permits you to cast a pointer ... garbage out" is the rule that applies here. ... You mean that on this compiler, ...
    (comp.lang.c)
  • Re: File performance
    ... but the main problem is that although I call dispose on the ... filestream, and I expect the garbage handle will not run immediatly, the ... It doesn't give any out of memory ...
    (microsoft.public.dotnet.framework.performance)
  • Re: How to return VB "Nothing"
    ... That last advice is wrong. ... The fact that the garbage in the ... Microsoft MVP, MCSD ... > If fact the pointer that VB will have created to receive the return value ...
    (microsoft.public.vc.atl)