Re: Does This Leak Memory?
From: Alexander Nickolov (agnickolov_at_mvps.org)
Date: 10/19/04
- Next message: Jochen Kalmbach: "Re: dll listing needed"
- Previous message: William DePalo [MVP VC++]: "Re: dll listing needed"
- In reply to: Carl Daniel [VC++ MVP]: "Re: Does This Leak Memory?"
- Next in thread: Larry Brasfield: "Re: Does This Leak Memory?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 19 Oct 2004 10:10:31 -0700
I guess it is meant to be called like this:
cpConn->Open(L"whatever", L"user", L"pass", -1);
Then there will be only one set of temporaries on the stack.
-- ===================================== Alexander Nickolov Microsoft MVP [VC], MCSD email: agnickolov@mvps.org MVP VC FAQ: http://www.mvps.org/vcfaq ===================================== "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam> wrote in message news:eRBpRJftEHA.2536@TK2MSFTNGP11.phx.gbl... > Lawrence Groves wrote: >> Hi all, >> >> 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. >> Something to do with the underlying BSTR not being cleaned up properly >> because of the way the class's copy constructor works. >> >> Am I loosing it? It seems to me that the first piece of code would't >> leak, but I would like a second opinion. > > They're both fine, barring any compiler bugs that might cause one to leak. > Both require construction and destruction of _bstr_t instances in > automatic storage (the stack), so if there's a problem with the _bstr_t > class's copy constructor it'll affect both scenarios equally. > > The connection function really ought to be declared > > Open ( const _bstr_t& ConnectionString, const _bstr_t& UserID, const > _bstr_t& Password ... > > as that would actually eliminate the need to call the copy constructor, > but I assume you don't have any control over that (#import generated code, > right?). > > -cd > >
- Next message: Jochen Kalmbach: "Re: dll listing needed"
- Previous message: William DePalo [MVP VC++]: "Re: dll listing needed"
- In reply to: Carl Daniel [VC++ MVP]: "Re: Does This Leak Memory?"
- Next in thread: Larry Brasfield: "Re: Does This Leak Memory?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|