Article View: borland.public.cppbuilder.nativeapi
Article #2504Re: Memory Leak - TForm or TStringGrid or DynamicArray
From: Remy Lebeau (TeamB)
Date: Thu, 08 Feb 2007 23:49
Date: Thu, 08 Feb 2007 23:49
70 lines
3274 bytes
3274 bytes
"Dan Mayo" <do_not_spam_dmayo2@netzero.com> wrote in message
news:45cba995$1@newsgroups.borland.com...
> Ok, I have went thru all of my code and have changed to auto_ptrs
for
> the most part; and in places where I still need to use the pointer,
I
> have been sure to initialize it so that I don't attempt to delete a
> pointer that hasn't been initialized.
You shouldn't be deleting anything when using auto_ptr. That is the
whole point of auto_ptr - it frees the object for you automatically.
> std::auto_ptr<TSubForm> newform(TSubForm(this));
That is not a valid statement. You can't create VCL objects on the
stack. Perhaps you meant to call the 'new' operator instead?
std::auto_ptr<TSubForm> newform(new TSubForm(this));
> On a side note, is the following valid?
Yes (after you fix the above bug), though I would sggest you use the
auto_ptr's get() method instead:
void function()
{
std::auto_ptr<TSubForm> form(new TSubForm(NULL));
SubFormICanReferenceSomewhereElse = form.get(); // <--
DoSomething();
SubFormICanReferenceSomewhereElse = NULL;
}
Of course, if DoSomething() throws an exception, then
SubFormICanReferenceSomewhereElse will not be reset to NULL, though
the TForm will have been freed. You would need an exception handler
to ensure it gets reset correctly:
void function()
{
std::auto_ptr<TSubForm> form(new TSubForm(this));
SubFormICanReferenceSomewhereElse = form.get();
try
{
DoSomething();
}
catch(...)
{
}
SubFormICanReferenceSomewhereElse = NULL;
}
It would be better to pass the TForm pointer as a parameter instead of
using a global:
void DoSomething(TSubForm *Form)
{
if( Form )
// do something with the Form
}
void function()
{
std::auto_ptr<TSubForm> form(new TSubForm(NULL));
DoSomething(form.get());
}
Gambit
Technical Details
Message-ID:
<45cbb722$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<45c7810f@newsgroups.borland.com> <45c7dc8d$1@newsgroups.borland.com> <45c89ec6@newsgroups.borland.com> <45ca4546$1@newsgroups.borland.com> <0sqks2t4c90epuc62bv635h36cqeat0ujd@4ax.com> <45cb3451$1@newsgroups.borland.com> <ptems2p2k2rquqdiao805ik2fhg6jl9n9a@4ax.com> <45cb5f6b@newsgroups.borland.com> <45cba995$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,191a7d21d69bde32 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <45c7810f@newsgroups.borland.com> <45c7dc8d$1@newsgroups.borland.com> <45c89ec6@newsgroups.borland.com> <45ca4546$1@newsgroups.borland.com> <0sqks2t4c90epuc62bv635h36cqeat0ujd@4ax.com> <45cb3451$1@newsgroups.borland.com> <ptems2p2k2rquqdiao805ik2fhg6jl9n9a@4ax.com> <45cb5f6b@newsgroups.borland.com> <45cba995$1@newsgroups.borland.com> Subject: Re: Memory Leak - TForm or TStringGrid or DynamicArray Date: Thu, 8 Feb 2007 15:49:12 -0800 Lines: 72 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 75.83.34.171 Message-ID: <45cbb722$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1170978594 75.83.34.171 (8 Feb 2007 15:49:54 -0800) Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail