Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2007

Tip: Looking for answers? Try searching our database.

gcnew,

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dogbert1793@yahoo.com - 13 Nov 2007 17:28 GMT
Hello,

Does one need to delete pointers allocated with gcnew?  Or does the
garbage collector do it?

I always though the garbage collector did it, but I saw this example
today on MSDN:

StreamReader^ sr = gcnew StreamReader( "TestFile.txt" );
     try
     {
        String^ line;

        // Read and display lines from the file until the end of
        // the file is reached.
        while ( line = sr->ReadLine() )
        {
           Console::WriteLine( line );
        }
     }
     finally
     {
        if ( sr )
           delete (IDisposable^)sr;   // <-----DELETE HERE ???
     }
Jochen Kalmbach [MVP] - 13 Nov 2007 19:08 GMT
Hi dogbert1793!

> Does one need to delete pointers allocated with gcnew?  Or does the
> garbage collector do it?

gcnew => Garbage Collector NEW

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

Ben Voigt [C++ MVP] - 13 Nov 2007 19:56 GMT
> Hello,
>
[quoted text clipped - 21 lines]
>            delete (IDisposable^)sr;   // <-----DELETE HERE ???
>      }

That's just calling Dispose... not freeing the memory.

Also, that example would be much better using stack semantics:

StreamReader sr("TestFile.txt");
String^ line;
while (nullptr != (line = sr->ReadLine()))
   Console::WriteLine(line);
/* automatic cleanup by compiler for all exit paths, including exceptions */
Mark Salsbery [MVP] - 13 Nov 2007 20:42 GMT
In addition to Ben's reply, you may want to take a look at this...

Destructors and Finalizers in Visual C++
http://msdn2.microsoft.com/en-us/library/ms177197(VS.80).aspx

Mark

Signature

Mark Salsbery
Microsoft MVP - Visual C++

> Hello,
>
[quoted text clipped - 21 lines]
>            delete (IDisposable^)sr;   // <-----DELETE HERE ???
>      }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.