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

Posted on 2006-04-26
Last Modified: 2008-01-09
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 = L"CN=SymarkGroup,CN=USERS," +  bstrDomainOnly;
Question by:lapucca
    LVL 86

    Expert Comment

    What data type is 'syUserContextPath'?
    LVL 12

    Expert Comment

    Can you please elaborate.
    LVL 48

    Accepted Solution

    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.
    LVL 1

    Expert Comment

    Try doing this:-
    BSTR bs = syUserContextPath.Detach();
    Now free the bstr called "bs":-

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


    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now