_bstr_t这么用发生内存泄露吗? 
请说明理由.
1>
_bstr_t sTemp    = L"aaaaa";
2>
_bstr_t sTemp;
sTemp    = L"aaaaa";

解决方案 »

  1.   

    都不会,常量L"aaaaa"所占用的内存是不可避免的。
    随着作用域的结束,
    _bstr_t 的析构函数会清除所占用的内存。inline _bstr_t::~_bstr_t() throw()
    {
    _Free();
    }
    inline void _bstr_t::Data_t::_Free() throw()
    {
    if (m_wstr != NULL) {
    ::SysFreeString(m_wstr);
    } if (m_str != NULL) {
    delete [] m_str;
    }
    }还有很多,你可以自己看 comutil.h
      

  2.   

    To: sunbo() 
    thanks a lot!but if I do like this,发生内存泄露?
    (!针对tmpbstr!)//Update static text with new value
    BSTR tmpBStr;m_pObject1->get_ObjectString(&tmpBStr);
    _bstr_t tmpbstr;tmpbstr= tmpBStr; 
    SetDlgItemText(IDC_CURPROPVAL, tmpbstr);
      

  3.   

    为什么不用::SysFreeString(tmpBStr); ?知道为什么内存泄露吗?
    // Assign a const wchar_t* to a _bstr_t
    //
    inline _bstr_t& _bstr_t::operator=(const wchar_t* s) throw(_com_error)
    {
    _Free();
    m_Data = new Data_t(s); return *this;
    }
    tmpBStr根本没有机会释放
      

  4.   

    为什么不用::SysFreeString(tmpBStr); ?知道为什么内存泄露吗?
    // Assign a const wchar_t* to a _bstr_t
    //
    inline _bstr_t& _bstr_t::operator=(const wchar_t* s) throw(_com_error)
    {
    _Free();
    m_Data = new Data_t(s); return *this;
    }
    tmpBStr根本没有机会释放
      

  5.   

    为什么不用::SysFreeString(tmpBStr); ?知道为什么内存泄露吗?
    // Assign a const wchar_t* to a _bstr_t
    //
    inline _bstr_t& _bstr_t::operator=(const wchar_t* s) throw(_com_error)
    {
    _Free();
    m_Data = new Data_t(s); return *this;
    }tmpBStr根本没有机会释放
      

  6.   

    Sorry:
    我的问题是
    针对tmpbstr的用法.用::SysFreeString(tmpBStr);后 就不发生内存泄露?
    也就是说,抛开tmpBStr的问题就不会发生内存泄露吗?
      

  7.   

    抛开tmpBStr的问题不会发生内存泄露试改为如下,可以去掉内存泄露
    //Update static text with new value
    BSTR tmpBStr;m_pObject1->get_ObjectString(&tmpBStr);
    _bstr_t tmpbstr(tmpBStr, false);SetDlgItemText(IDC_CURPROPVAL, tmpbstr);
      

  8.   

    sunbo() :
    谢谢你的回答,
    但,
    >抛开tmpBStr的问题不会发生内存泄露<------------1
    >试改为如下,可以去掉内存泄露  <--------------2 是指由tmpBStr     
    OR  tmpbstr `产生的泄露?  <<<<我的问题是针对tmpbstr的用法.>>>>
      

  9.   

    sunbo() :Yes,I'll 结贴,as soon as I think the answer satifies me.
      

  10.   

    “>抛开tmpBStr的问题不会发生内存泄露<------------1”
    这是针对你前面问题的回答。“>试改为如下,可以去掉内存泄露  <--------------2 是指由tmpBStr     
    OR  tmpbstr `产生的泄露?  ”指由tmpBStr产生的泄露,
    因为此时tmpBStr的内存由tmpbstr接管。tmpbstr析构时内存正确释放。“_bstr_t tmpbstr(tmpBStr, false);”
    是针对tmpbstr的用法,没看见吗?就你的问题作的回答我认为是够了,有问题你可以再问,
    希望你能言而有信,你至少该先给一些分了吧,兄弟!
      

  11.   

    sunbo() :
    谢谢你的回答,I dont think you really know the answer of problem I ask!
    To say frankly , the following is part of an article from MS.
    it says there is problem to use _bstr_t like that ...
    what I want to know is: if there is any difference from my case!I'll give you 一些分 for you help after I really get the answer
    from you or someone else.
    Anyway,I'll 结贴 in three days even if I can not get the correct answer.So, would like to wait a few days?
     
    again, thanks for your help.-----------------------------------------------------------------
    //Update static text with new value
    BSTR tmpBStr;m_pObject1->get_ObjectString(&tmpBStr);
    _bstr_t tmpbstr;tmpbstr= tmpBStr; //Caution: Memory leak occurs  <----pay attentionSetDlgItemText(IDC_CURPROPVAL, tmpbstr);
      

  12.   

    Do you known MSDN? Can you find the answer in the MS source file by yourself?
    I do all by myself. Do you have your own point of view?
    "tmpbstr= tmpBStr; //Caution: Memory leak occurs  <----pay attention"
    Caution? Yup. How to avoid it? 
    _bstr_t use a refence count. When you Assign tmpBStr, you clone it. This is the Key. My answer is
    "_bstr_t tmpbstr(tmpBStr, false);"
    The question is simple. and I got it. I waste many time to solve your problem in MSDN & Microsoft source file.
    And Now I give you some MSDN content._bstr_t( )   Constructs a default _bstr_t object that encapsulates a NULL BSTR object._bstr_t( BSTR bstr, bool fCopy )   Constructs a _bstr_t object from an existing BSTR (as opposed to a wchar_t* string). If fCopy is false, the supplied BSTR is attached to the new object without making a new copy via SysAllocString. This is the method used by the wrapper functions in the type library headers to encapsulate and take ownership of a BSTR, returned by an interface method, in a _bstr_t object. operator = Assigns a new value to an existing _bstr_t object. Dose this satisfy you?
      

  13.   

    "what I want to know is: if there is any difference from my case!
    "
    Your case is more simpler than that article from MS. I answer it first with the prove from MS source file.
    Do you understand?
      

  14.   

    真是好笑,有必要在此卖弄英文吗?中文都讲不清楚了吗?to xhuangp(老三) 
    你说的那篇文章,我找到了。你本质上问了两个问题,且我都回答过了。如果你不能从源码学到东西,你的水平永远只能如此了。提醒你一点,"SetDlgItemText(IDC_CURPROPVAL, tmpbstr);"对于"Unicode build"没有转换问题,而对于"Win32 build",却会发生转换,但不是内存泄漏,不会是英文能力不够吧?!请诸位朋友看看:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvc60/html/simpleclient.asp
    最后面的“BSTRs and String Manipulation”是他讲的部分,我也没有想到,我的解法与原文真是不谋而合。这部分本身也是没有错误的。让我气愤的是,他居然臆断为洋人讲的都是对的,还想以此佐证我的错误,问题是他连原文都没有搞清楚。这种求证精神?.....
    还是多一点中国人的自信吧!
      

  15.   

    To sunbo():
      (First of all , sorry to write to you in English again,since I'm not so convenient to write Chinese by my machine).
      I admire you so much for your knowing MSDN!?
      But never forget that when you want to answer a problem ,please make clear what the problem is, and remember just to copy some thing from MSDN can not make the perfect answer.
      I do not have anytime to quarrel with you at this pleace,If you really master or know something you can share with others as you like .
      Be a man ,sir!  I'll give you the tip (though I get the exact explanation by myself)as I told you. 
      Anyway,thanks for your help.    
       
      

  16.   

    To sunbo():
        
       If you really master or know something ,you can share with others as you like, Or,it's better for you to shut up!
    No one oblige you to do anything here,and you have no rights to
    be impolite here.
       Be a man ,sir!   I don't have anytime to quarrel with here.But pls remember,
    make clear what others say before you want to give an answer.
    and never forget that just to copy something from MSDN can not make a good answer.
       I will give you the TIP as I told you.(although,I get the exact answer by myself).
       Anyway,thanks a lot.
      

  17.   

    致 老三:如果你还会提问题的话,你应该学会提出准确的问题。
    如果你的中文表达有问题的话,你干脆纯用英文。
    如果因为你的能力还不足以问出有质量的问题,你最好闭嘴。
    如果你不能正确对待自已的缺点,你也最好闭嘴。不要以为别人与你一样,除了会从MSDN上拷贝外,没有独立思考的能力。
    做不到谦虚,不要在我面前谈绅士问题,你以为你是吗?照照镜子吧!假洋鬼子,不能尊重别人劳动和正视自已缺点的人!!!你还有信誉可言吗?
    你真正缺少的是信誉!!!如果真正弄懂了,还要在此提出如此简单的问题,对MSDN如此清楚的解释,还谈你的问题与MSDN文章的问题有什么不同,你不觉得你很好笑吗?谁是谁非,还是让广大网友来评说吧!