• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 859
  • Last Modified:

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;
0
lapucca
Asked:
lapucca
1 Solution
 
jkrCommented:
What data type is 'syUserContextPath'?
0
 
rajeev_devinCommented:
Can you please elaborate.
0
 
AlexFMCommented:
Detach is opposed to Attach. This function doesn't release wrapped BSTR, it returns it to caller which is responsible to release it. Try to remove this line, I think operator= takes care about releasing previous content.
0
 
simplysuperb_knpCommented:
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().

0
 
lapuccaAuthor Commented:
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now