CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    42

    Cleaning up strings

    Hi all,

    Given this code, I want to make sure that there are no leaks from the strings that I have used:

    #include <atlstr.h>

    ...

    string varVarName = GetAttribute("VarIn", pNodeMap);
    string stringIn = getDictionary(varVarName);
    CString cstrIn = CString(stringIn.c_str());
    cstrIn.Trim();
    CT2CA pszConvertedAnsiString(cstrIn);
    std::string strOut(pszConvertedAnsiString);


    From what I understand, I think that I do not need to free std::strings as the program automatically frees those. I am not sure regarding CStrings, although I also believe that the program handles those.

    What I know is that from the many string types in C++, only BSTR require freeing if you have called AllocSysString on it or equate it to NULL.

    Can you help me understand why you need to free BSTR and not std::string, CString, etc.?

    Thank you!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Cleaning up strings

    Quote Originally Posted by LeanA View Post
    From what I understand, I think that I do not need to free std::strings as the program automatically frees those
    Not quite. It isn't the program that frees the std::string, it's the std::string class itself that does the "freeing" when the variable goes out of scope. When the destructor of std::string is invoked, the memory that the std::string allocated is deallocated automatically.
    I am not sure regarding CStrings, although I also believe that the program handles those.
    Again, it isn't the "program" doing this -- it's CString's desctructor that does this.
    What I know is that from the many string types in C++, only BSTR require freeing if you have called AllocSysString on it or equate it to NULL.
    Well, this goes back to my earlier statements.

    BSTR is not a C++ class, therefore it knows nothing by itself of when it goes out of scope or will not be used anymore.

    Regards,

    Paul McKenzie

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Cleaning up strings

    Well, BSTR is not convenient enough to work with.
    _bstr_t class (a wrapper of BSTR) is much better in use.
    Victor Nijegorodov

  4. #4
    Join Date
    Mar 2010
    Posts
    42

    Re: Cleaning up strings

    Hi Paul, and Victor, thanks for replying.

    Thankyou for those inputs! I'll keep them in mind.

    Thank you very much!

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Cleaning up strings

    Off topic to the question that you posed:

    It looks like you are:

    1) taking a std::string and converting it to a CString
    2) trimming the CString
    3) converting back to a std::string

    Why not just trim the std::string directly ?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured