473,471 Members | 2,040 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 11886
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: 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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.