Note that there are some explanatory texts on larger screens.

plurals
  1. POFreeing a BSTR using ::SysFreeString(). More Platform Dependant?
    primarykey
    data
    text
    <p>I am writing a COM Server which have a plenty of Interfaces and methods. And most of the methods have the BSTR as the parameters and as local parameters used for the return. A snippet looks like</p> <p><strong>Update 5:</strong></p> <p>The real code. This fetches from bunch of Data based on a specific condition the DB to populate an array of Object.</p> <pre><code>STDMETHODIMP CApplication::GetAllAddressByName(BSTR bstrParamName, VARIANT *vAdddresses) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) //check the Database server connection COleSafeArray saAddress; HRESULT hr; // Prepare the SQL Strings dan Query the DB long lRecCount = table.GetRecordCount(); if (lRecCount &gt; 0) { //create one dimension safe array for putting details saAddress.CreateOneDim(VT_DISPATCH,lRecCount); IAddress *pIAddress = NULL; //retrieve details for(long iRet = table.MoveFirst(),iCount=0; !iRet; iRet = table.MoveNext(),iCount++) { CComObject&lt;CAddress&gt; *pAddress; hr = CComObject&lt;CAddress&gt;::CreateInstance(&amp;pAddress); if (SUCCEEDED(hr)) { BSTR bstrStreet = ::SysAllocString(table.m_pRecordData-&gt;Street); pAddress-&gt;put_StreetName(bstrStreet); BSTR bstrCity = ::SysAllocString(table.m_pRecordData-&gt;City); pAddress-&gt;put_CityName(bstrCity); } hr = pAddress-&gt;QueryInterface(IID_IAddress, (void**)&amp;pIAddress); if(SUCCEEDED(hr)) { saAddress.PutElement(&amp;iCount,pIAddress); } } *vAdddresses=saAddress.Detach(); } table.Close(); return S_OK; } STDMETHODIMP CAddress::put_CityName(BSTR bstrCityName) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) // m_sCityName is of CComBSTR Type m_sCityName.Empty();//free the old string m_sCityName = ::SysAllocString(bstrCityName);//create the memory for the new string return S_OK; } </code></pre> <p>The problem lies in the Memory Freeing part. The code works very fine in any Win XP machines, but when comes to WIN2K8 R2 and WIN7 the code crashes and pointing to the ::SysFreeString() as the culprit. The MSDN is not adequate to the solution.</p> <p>Can anyone please help in finding the right solution? </p> <p>Thanks a lot in advance :)</p> <p><strong>Update 1:</strong></p> <p>I have tried using the CComBSTR as per the suggestion in the place of raw BSTR, initialized using direct CString's and excluded the SysFreeString(). But for my trouble, on getting out of scope the system is calling the SysFreeString() which again causes the crash :(</p> <p><strong>Update 2:</strong> With the same CComBSTR i tried to allocate using the SysAllocString() , the problem remains same :(</p> <p><strong>Update 3:</strong> I am tired of all the options and in peace I am having only question in mind</p> <blockquote> <p>Is it necessary to free the BSTR through SysFreeString() which was allocated using SysAllocString()/string.AllocSysString()?</p> </blockquote> <p><strong>Update 4:</strong> I missed to provide the information about the crash. When I tried to debug the COM server crashed with a error saying </p> <blockquote> <p>"Possible Heap Corruption"</p> </blockquote> <p>. Please help me out of here.. :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload