How to get access on TMemo Canvas ?

How to get access on TMemo Canvas ?

Post by Team » Sun, 29 Jun 2003 13:13:41



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 - http://bcbcaq.bytamin-c.com

[snip]

Quote:> Why do you need to use std::auto_ptr?

 
 
 

How to get access on TMemo Canvas ?

Post by Steve_Alett » Tue, 01 Jul 2003 19:00:12




Quote:> 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

Absolutely clear Damon, thanks.

Steve.

 
 
 

How to get access on TMemo Canvas ?

Post by Steve_Alett » Tue, 01 Jul 2003 20:56:55




Quote:> 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

All clear Damon, but I have another question: it seems
auto_ptr is more powerful than new/delete, so, why or when
to use new/delete?

As I asked to Hans in an earlier post, what do you use
normally? New/delete or auto_ptr? I'm used to use
new/delete, but if there's a better choice...

TIA,

Steve.

 
 
 

How to get access on TMemo Canvas ?

Post by Team » Wed, 02 Jul 2003 11:46:38


Steve,

Quote:> All clear Damon, but I have another question: it seems
> auto_ptr is more powerful than new/delete, so, why or when
> to use new/delete?

There are cases in which you shouldn't use the auto_ptr class.  For example,
because auto_ptr deletes the memory using the 'delete' operator, you don't
want to use auto_ptr with pointers whose memory needs to be deleted with
'delete []' (e.g., when allocating an array with 'new []'; see
http://www.gotw.ca/gotw/042.htm)  In addition, you have to be careful when
copying auto_ptr's because ownership is transferred from the copied object
to the copying object.  For example...

class TFoo
{
public:
  TFoo() { ShowMessage("Creating TFoo"); }
  ~TFoo() { ShowMessage("Destroying TFoo"); }

Quote:};

#include <memory>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TFoo* RawFoo = new TFoo();
  std::auto_ptr<TFoo> AutoFoo(RawFoo);
  {
    std::auto_ptr<TFoo> AutoFoo2 = AutoFoo;
  }
  ShowMessage("End of inner block");
  // At this point...
  // (1) AutoFoo2 has called 'delete RawFoo'.
  // (2) 'AutoFoo.get()' reports NULL because
  //     ownership has transferred to AutoFoo2.
  // (3) Don't dereference RawFoo because
  //     AutoFoo2 has destroyed it!

Quote:}

Here, ownership of RawFoo is transferred first to AutoFoo, and then to
AutoFoo2.  At the end of the inner block where AutoFoo2 goes out of scope,
you need to be careful not to access RawFoo because it has been destroyed by
AutoFoo2.  The output of the above code should be "Creating TFoo",
"Destroying TFoo", and then "End of inner block".  Ownership transfer is
also the reason you shouldn't use auto_ptrs with container classes that
might make internal copies (e.g., upon a sort of the items); see
http://www.gotw.ca/gotw/025.htm.  The Boost library has some smart-pointer
classes that address this (and the array) issue(s):
http://www.boost.org/libs/smart_ptr/.

Quote:> As I asked to Hans in an earlier post, what do you use
> normally? New/delete or auto_ptr? I'm used to use
> new/delete, but if there's a better choice...

I normally use auto_ptr when possible; for arrays and containers, I use new
and delete.  

--
Damon (TeamB)
BCBCAQ - http://bcbcaq.bytamin-c.com

 
 
 

How to get access on TMemo Canvas ?

Post by Steve_Alett » Wed, 02 Jul 2003 16:11:15




Quote:> There are cases in which you shouldn't use the auto_ptr

class.  For example,

Many thanks.

Steve.