Board index » cppbuilder » R: How to get access on TMemo Canvas ?

R: How to get access on TMemo Canvas ?


2003-06-27 08:02:51 PM
cppbuilder73
Giuliano < XXXX@XXXXX.COM >wrote in message
XXXX@XXXXX.COM ...
Quote
On Fri, 27 Jun 2003 11:16:35 +0200, Hans Galema
< XXXX@XXXXX.COM >wrote:

#include <memory>

std::auto_ptr<TControlCanvas>C( new TControlCanvas );
I've understood the matter, but I have some related
questions...
Why do you need to use std::auto_ptr?
Would it be the same with TControlCanvas *C = new
TControlCanvas()?
What differences are there?
What's the best way?
TIA,
Steve.
 
 

Re:R: How to get access on TMemo Canvas ?

Steve_Aletto wrote:
Quote
Why do you need to use std::auto_ptr?
Would it be the same with TControlCanvas *C = new
TControlCanvas()?
What differences are there?
The difference is the memorymanager. With auto_ptr you don't have to
bother about freeing the memory when it goes out of scope. With new
you have to use delete (exept if the object handles that self).
Quote
What's the best way?
Do your choice.
Hans.
 

Re:R: How to get access on TMemo Canvas ?

Steve,
The auto_ptr class assures that the TControlCanvas object is destroyed
when the variable (C, in this case) goes out of scope. It's important to
destroy the TControlCanvas object not only to avoid leaking its associated
memory, but also to ensure that device context is released via the
ReleaseDC() Win32 function (which the TControlCanvas class effectively calls
from within its destructor). Furthermore, with auto_ptr, you don't have to
worry about explicitly calling 'delete C' in case an exception is thrown.
For example, the following blocks are functionally equivalent...
TControlCanvas* C = new TControlCanvas();
try
{
C->Control = Memo1;
// do something with C
}
catch (...)
{
delete C;
throw;
}
delete C;
or,
TControlCanvas* C = new TControlCanvas();
try
{
C->Control = Memo1;
// do something with C
}
__finally
{
delete C;
}
or,
std::auto_ptr<TControlCanvas>C(new TControlCanvas());
C->Control = Memo1;
// do something with C
HTH,
--
Damon (TeamB)
BCBCAQ - bcbcaq.bytamin-c.com
Steve_Aletto wrote:
[snip]
Quote
Why do you need to use std::auto_ptr?
 

{smallsort}