Results 1 to 2 of 2

Thread: STDMETHODIMP get_Text

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    STDMETHODIMP get_Text

    STDMETHODIMP CMSTCtl::get_Text(BSTR* pVal)
    {
    _bstr_t bText = m_EditCtl.GetText();
    *pVal = bText.GetBSTR();
    return S_OK;
    }

    STDMETHODIMP CMSTCtl::get_Text(BSTR* pVal)
    {
    _bstr_t bText = m_EditCtl.GetText();
    *pVal = bText.copy();
    return S_OK;
    }

    STDMETHODIMP CMSTCtl::get_Text(BSTR* pVal)
    {
    _bstr_t bText = m_EditCtl.GetText();
    *pVal = bText;
    return S_OK;
    }

    there're three ways to set this public property of Text in ATL, which is the best without memory leak?
    Last edited by DaveDavis; Apr 20th, 2023 at 08:43 PM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: STDMETHODIMP get_Text

    I am not sure. ChatGPT gives different and confused answers.

    _bstr_t PropFileName = L"MyConfigures.xml";

    STDMETHODIMP CMyControl::get_FileName(BSTR* pVal)
    {
    *pVal = PropFileName.copy();
    return S_OK;
    }

    ChatGPT said:

    In the code you provided, there is no memory leak when assigning *pVal = PropFileName.copy(). The copy method of the _bstr_t class allocates memory for a copy of the string, and the memory is managed by the _bstr_t object.

    When you assign *pVal = PropFileName.copy(), you are assigning the copied string to the BSTR pointer that was passed to the function. The memory allocated for the copied string will be automatically released when the _bstr_t object goes out of scope at the end of the function.

    However, it's worth noting that the caller of this function is responsible for freeing the memory allocated for the BSTR pointer by calling SysFreeString. So make sure the caller of this function is aware of this and properly frees the memory.
    Yes, there is a potential memory leak in the code you provided.

    When you call the copy() method of a _bstr_t object, it creates a new BSTR string and returns a pointer to it. This pointer is then assigned to *pVal, which is an out parameter. The caller of this method is responsible for releasing the memory allocated for the returned BSTR string.

    If the caller fails to release the memory, it can result in a memory leak. To prevent this, you should modify the code to use SysAllocString() to allocate a new BSTR string and copy the contents of PropFileName to it. This way, the caller of the get_FileName() method will be responsible for releasing the memory allocated for the BSTR string.

    Here's how you can modify the code:
    Code:
    STDMETHODIMP CMyControl::get_FileName(BSTR* pVal)
    {
        *pVal = SysAllocString(PropFileName);
        if (*pVal == nullptr)
        {
            return E_OUTOFMEMORY;
        }
        return S_OK;
    }
    This code uses SysAllocString() to allocate a new BSTR string and copies the contents of PropFileName to it. If the allocation fails, it returns E_OUTOFMEMORY. The caller of this method is responsible for releasing the memory allocated for the returned BSTR string using SysFreeString().
    The _bstr_t::copy() method returns a new BSTR object that is a copy of the _bstr_t object it is called on. In this case, it creates a new BSTR object that contains the same string as PropFileName.

    When you assign this copied BSTR object to *pVal, you are effectively passing ownership of the object to the caller of the get_FileName method. The caller is responsible for freeing the memory allocated for the BSTR object using the SysFreeString function.

    Therefore, there is no memory leak in the code snippet you provided as long as the caller of get_FileName properly frees the BSTR object passed to it.
    Last edited by DaveDavis; May 12th, 2023 at 09:13 PM.

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