473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

gcnew,

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 ???
}

Nov 13 '07 #1
3 11883
Hi dogbert1793!
Does one need to delete pointers allocated with gcnew? Or does the
garbage collector do it?
gcnew =Garbage Collector NEW
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 13 '07 #2

<do*********@yahoo.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
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 ???
}
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 */
Nov 13 '07 #3
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/lib...97(VS.80).aspx

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++
<do*********@yahoo.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
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 ???
}
Nov 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: cedgington | last post by:
I wanted to take advantage of the large set of functionality offered by the framework, so for my latest project I'm using managed C++ with .NET v2. I'm using the gcnew operator in two different...
3
by: hedbonker | last post by:
OK - I am new to .net C++. Trying to write a simple app that creates an XML output file based on some values that a user puts in a form. After looking in the help, the sample code provided was...
0
by: pkolinko | last post by:
Hi everyone, I am writing a small low level embedded USB application using C++/CLI windows Forms. I am sort of new to the C++/CLI and having trouble understanding what happens in this very...
6
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my...
0
by: whm | last post by:
The page http://msdn2.microsoft.com/en-us/708fb7c4(VS.80).aspx has this code: ServiceController^ sc = gcnew ServiceController; if ( sc ) { ... Section 15.4.6 in ECMA 372 (the C++/CLI spec)...
2
by: Vivienne | last post by:
Hi, I have a question about "gcnew". I am a C# library in my c++ project, so "gcnew" is often used when calling the managed methods. so here are my questions: are new memories allocated by when...
0
by: dogbert1793 | last post by:
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: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.