Board index » cppbuilder » Release() vs. delete

Release() vs. delete


2005-12-26 04:38:11 AM
cppbuilder70
On Borland's site (developer network), there is a drag/drop code:
bdn.borland.com/article/0,1410,26545,00.html
// relevant part:
__fastcall TGenericVCLDropTarget::~TGenericVCLDropTarget()
{
m_dt->Release();
//delete m_dt;
OleUninitialize();
}
In my BCB, when I use: Release(), the CG reports Access to freed memory or
something similar, while with delete, which is under comments it works
without
CG notifications...
What is the difference...
--
Best regards,
Vladimir Stefanovic
 
 

Re:Release() vs. delete

At 21:38:11, 25.12.2005, Vladimir Stefanovic wrote:
Quote
On Borland's site (developer network), there is a drag/drop code:
bdn.borland.com/article/0,1410,26545,00.html

// relevant part:

__fastcall TGenericVCLDropTarget::~TGenericVCLDropTarget()
{
m_dt->Release();
//delete m_dt;
OleUninitialize();
}

In my BCB, when I use: Release(), the CG reports Access to freed memory
or something similar, while with delete, which is under comments it
works without CG notifications...

What is the difference...
Release() uses messages, and gives the class time to close down properly,
after the message handler has finished. It is not a good idea to use
delete from a message handler of the class you are trying to delete (the
code calling the message handler might tr to access members of that
class). Otherwise, you can use delete.
AFAIK, the online help has an article about that.
--
Rudy.Velthuis {TeamB} velthuis.homepage.t-online.de/
"Getting an education was a bit like a communicable {*word*225} disease.
It made you unsuitable for a lot of jobs and then you had the urge
to pass it on." -- Terry Pratchett, Hogfather
 

Re:Release() vs. delete

"Vladimir Stefanovic" < XXXX@XXXXX.COM >wrote in message
Quote
In my BCB, when I use: Release(), the CG reports Access to
freed memory or something similar
You did not show the code that is assigning a value to m_dt. You are likely
calling Release() on a NULL pointer.
Quote
while with delete, which is under comments it works without CG
notifications...
'delete' ignores NULL pointers.
Quote
What is the difference...
In the example, m_dt is a pointer to an instance of a class that derives
from an Interface. Never never never call 'delete' on an Interface pointer.
Calling the Interface's Release() method is the only way to safely free the
object. Interfaces are referece-counted, where AddRef() increments the
reference count, and Release() decrements it. The object will automatically
free its own memory when its reference count falls to 0. So, for CG to
report an error on Release(), the object is likely being freed too early,
such as by its reference count being mismanaged.
Gambit
 

{smallsort}

Re:Release() vs. delete

"Rudy Velthuis [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
Release() uses messages, and gives the class time to close down properly
You are thinking of the TForm's Release() method. That is not what the code
is actually using. See my other reply.
Gambit
 

Re:Release() vs. delete

Quote
Never never never call 'delete' on an Interface pointer.
Thank you.
--
Best regards,
Vladimir Stefanovic