Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

How do I assign new BSTR string to a _bstr_t that already holds an address without getting a memory leak?

Hi Expert,

I am getting memory leak with my code below.  I just want to assign a new BSTR string to a _bstr_t variable but I'm getting a memory leak report in Rational Purfify from it.  Is the Detatch method a wrong call?  What is the appropriat way to do this?  Thank you.
syUserContextPath is a _bstr_t variable and so is bstrDomainOnly

                  syUserContextPath.Detach();
                  syUserContextPath = L"CN=SymarkGroup,CN=USERS," +  bstrDomainOnly;
Avatar of jkr
jkr
Flag of Germany image

What data type is 'syUserContextPath'?
Avatar of rajeev_devin
rajeev_devin

Can you please elaborate.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try doing this:-
BSTR bs = syUserContextPath.Detach();
Now free the bstr called "bs":-
SysFreeString(bs);

I guess what is happening is that you are not freeing the memory after a call to Detach().

Avatar of lapucca

ASKER

As I mentioned in my posting:

syUserContextPath is a _bstr_t variable and so is bstrDomainOnly

It's my understanding that _bstr_t is a smart pointer that it manages its own memory.  I don't think that's an issue.  I just want to assign it a new BSTR string.  I do find that it seems to work that I do a Detach() before I assign the new string, otherwise my program crashes.  I don't know if this is the right way to reasign string to this type of variable.

Thank you.