Re: Does This Leak Memory?

From: Larry Brasfield (donotspam_larry_brasfield_at_hotmail.com)
Date: 10/19/04


Date: Tue, 19 Oct 2004 08:48:47 -0700


"Lawrence Groves" <lgroves@ducost.deleteme.com> wrote in message news:OnPJC9etEHA.3564@tk2msftngp13.phx.gbl...
> Hi all,
Hi.

> A coleague of mine told me something strange about some code of mine that has run for years (not necessarily without error). I
> pass a string to an ADO connection object's open method, who's prototype is:
>
> inline HRESULT Connection15::Open ( _bstr_t ConnectionString, _bstr_t UserID, _bstr_t Password, long Options )
>
> I call the routine like so:
>
> _bstr_t bsConn = "whatever";
>
> cpConn->Open(bsConn, _bstr_t("user"), _bstr_t("pass"), -1);
>
> but he says I should do the following to avoid a memory leak:
>
> _bstr_t bsConn = "whatever";
>
> _bstr_t bsUser("user");
> _bstr_t bsPass("pass");
>
> cpConn->Open(bsConn, bsUser, bsPass, -1);
>
> He claims that the two temporaries in the first case may leak memory.

That would depend on the compiler. A defective
one *might* leak memory by mishandling temporary
objects. To my knowledge, MSVC++ does not
suffer that particular defect. Nor have I heard of
other C++ compilers with such a defect. Can your
colleague name a C++ compiler that does?

> Something to do with the underlying BSTR not being cleaned up properly because of the way the class's copy constructor works.

I would be quite surprised to find that such a basic
bug would survive until the Nth generation of the
library defining _bstr_t. In fact, I would bet real
money against such a proposition.

> Am I loosing it? It seems to me that the first piece of code would't leak, but I would like a second opinion.

Unlikely. But why do you wonder? Have you not
yet tested your code for leaks? Perhaps you should
explore why so many people do that and how easy
it is. Then do it.

> TIA, Loz.

You're welcome.

-- 
--Larry Brasfield
email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me. 


Relevant Pages

  • Re: Does This Leak Memory?
    ... > I call the routine like so: ... > He claims that the two temporaries in the first case may leak memory. ... That would depend on the compiler. ... other C++ compilers with such a defect. ...
    (microsoft.public.vc.language)
  • Re: delete
    ... >compiler so defective as to allow it in the first place is likely to ... that would enable your admitted "poor idea". ... You know, some things are better left unsaid, such as this idea to overload ... >use of this particular defect is a bad idea anyway. ...
    (microsoft.public.vc.language)
  • Re: pointer components and memory leaks
    ... that's the same thing as what Paul said. ... Let's not suggest compiler bug every time one finds a problem. ... leak memory, though even there, user bugs in other parts of the code can ...
    (comp.lang.fortran)
  • Re: pointer components and memory leaks
    ... that's the same thing as what Paul said. ... Let's not suggest compiler bug every time one finds a problem. ... leak memory, though even there, user bugs in other parts of the code can ...
    (comp.lang.fortran)
  • Re: delete
    ... > ambiguities when you tried to use it: ... compiler so defective as to allow it in the first place is likely to ... imagine the effects of moving to a newer one. ... use of this particular defect is a bad idea anyway. ...
    (microsoft.public.vc.language)