2013-01-23 31 views

回答

3

在使用調試器執行實施之後,我的結論是,沒有必要手動將返回的字符串delete/free。返回字符串的生命週期在內部由_bstr_t管理。

請參見下面的代碼片段從實現:

// Extract a const char_t* 
// 
inline _bstr_t::operator const char*() const throw(_com_error) 
{ 
    return (m_Data != NULL) ? m_Data->GetString() : NULL; 
} 

inline const char* _bstr_t::Data_t::GetString() const throw(_com_error) 
{ 
    if (m_str == NULL) { 
     m_str = _com_util::ConvertBSTRToString(m_wstr); 

     if (m_str == NULL && m_wstr != NULL) { 
      _com_issue_error(E_OUTOFMEMORY); 
     } 
    } 

    return m_str; 
} 

inline void _bstr_t::Data_t::_Free() throw() 
{ 
    if (m_wstr != NULL) { 
     ::SysFreeString(m_wstr); 
    } 

    if (m_str != NULL) { 
     delete [] m_str; 
    } 
} 

它也還可以使用無名_bstr_t如下因爲_bstr_t實例後的CString構造已經完成銷燬

CString abc((LPCTSTR)_bstr_t(OLESTR("ABC"))); 
AfxMessageBox(abc);