Click here to Skip to main content
15,923,281 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralSTL error Pin
mansour_ahmadian15-Jan-05 1:30
mansour_ahmadian15-Jan-05 1:30 
GeneralRe: STL error Pin
Michael Dunn15-Jan-05 10:29
sitebuilderMichael Dunn15-Jan-05 10:29 
GeneralCustomising Connection Tab in data link dialog Box Pin
Member 1266497613-Jan-05 20:44
Member 1266497613-Jan-05 20:44 
GeneralOnSize : Positioning Static Controls Pin
tnguyen44412-Jan-05 12:40
tnguyen44412-Jan-05 12:40 
GeneralRe: OnSize : Positioning Static Controls Pin
SteveKing14-Jan-05 1:04
SteveKing14-Jan-05 1:04 
GeneralAccessing IwebBrowser2 outside toolbar class Pin
tnguyen44412-Jan-05 12:11
tnguyen44412-Jan-05 12:11 
GeneralSorry! ATL : get_, put_, CString and BSTR Pin
Malcolm Smart11-Jan-05 9:34
Malcolm Smart11-Jan-05 9:34 
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Martin Koorts12-Jan-05 1:19
Martin Koorts12-Jan-05 1:19 
Hi
I have 3 points to make;

1. Looking at the following code;

STDMETHODIMP CFamily::put_FirstName(BSTR newVal)
{
_firstname.m_str = newVal;
return S_OK;
}

You should use the CComBSTR's assignment operator, like so;

STDMETHODIMP CFamily::put_FirstName(BSTR newVal)
{
_firstname = newVal; // makes a copy of (the client allocated) newVal, and SysFrees the old string in _firstname.m_str
return S_OK;
}

Otherwise, by just calling _firstname.m_str = newVal, you end up leaking the (old) value in _firstname.m_str and taking ownership of the client allocated string newVal and end up calling SysFree in both the COM server and client.


2. You're leaking a BSTR when calling PutFirstName;

void CComTesterDlg::OnSetFirstName()
{
UpdateData();
::CoInitialize(NULL);
IFamilyPtr first;
HRESULT hr = first.CreateInstance(__uuidof(Family));
if (SUCCEEDED(hr))
first->PutFirstName(m_FirstName.AllocSysString());
first = NULL;
::CoUninitialize();
}

You're calling AllocSysString, without a corresponding SysFreeString. If you're passing a BSTR to a COM client as a read-only (IDL in) parameter, the client both allocates and frees the BSTR, if the server wishes to hold on to it, it should make a copy (as demonstrated in point 1.)

So change the code to read;

void CComTesterDlg::OnSetFirstName()
{
UpdateData();
::CoInitialize(NULL);
IFamilyPtr first;
HRESULT hr = first.CreateInstance(__uuidof(Family));
if (SUCCEEDED(hr))
first->PutFirstName(CComBSTR(m_FirstName));
first = NULL;
::CoUninitialize();
}

where the CComBSTR class will both allocate and free the (client-side) BSTR.


3. Using smart pointers and CoUnitialize;

void CComTesterDlg::OnShowFirstName()
{
::CoInitialize(NULL);
IFamilyPtr first;
HRESULT hr = first.CreateInstance(__uuidof(Family));
if (SUCCEEDED(hr))
m_ShowFirstName = (char*)(first->GetFirstName());
::CoUninitialize();
UpdateData(FALSE);
}

The reason the ::CoUninitialize is causing problems there, is because the COM interface reference kept in the IFamilyPtr first has not been released yet. Change it to this;

void CComTesterDlg::OnShowFirstName()
{
::CoInitialize(NULL);
{
IFamilyPtr first;
HRESULT hr = first.CreateInstance(__uuidof(Family));
if (SUCCEEDED(hr))
m_ShowFirstName = (char*)(first->GetFirstName());
} // smartpointer's destructor (~IFamilyPtr) calls Release on the IFamily interface
::CoUninitialize(); // everything's released now, leave COM apartment
UpdateData(FALSE);
}

or (equivalent to calling 'first = NULL;')

void CComTesterDlg::OnShowFirstName()
{
::CoInitialize(NULL);
IFamilyPtr first;
HRESULT hr = first.CreateInstance(__uuidof(Family));
if (SUCCEEDED(hr))
m_ShowFirstName = (char*)(first->GetFirstName());
first.Release(); // smartpointer calls Release on the IFamily interface !! note the use of '.' and NOT '->' !!
::CoUninitialize(); // everything's released now, leave COM apartment
UpdateData(FALSE);
}


BTW: You can call ::CoInitialize once in CWinApp::InitInstance and ::CoUninitialize in ExitInstance, or use the MFC equivalent AfxOleInit in InitInstance only.

HTH
Martin
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Malcolm Smart12-Jan-05 3:14
Malcolm Smart12-Jan-05 3:14 
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Martin Koorts12-Jan-05 4:36
Martin Koorts12-Jan-05 4:36 
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Malcolm Smart12-Jan-05 5:10
Malcolm Smart12-Jan-05 5:10 
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Martin Koorts12-Jan-05 5:17
Martin Koorts12-Jan-05 5:17 
GeneralAdd Event for ATL based ActiveX Control Pin
greekgoddj10-Jan-05 21:43
greekgoddj10-Jan-05 21:43 
GeneralGDI+ Drawing shape on usercontrol shifts form up Pin
abcxyz8210-Jan-05 6:23
abcxyz8210-Jan-05 6:23 
Question&#xxx to character? Pin
Samsung10-Jan-05 2:30
Samsung10-Jan-05 2:30 
Generalie toolbar to block sites based on domain/ip address of the domain Pin
mr_mark_hewitt10-Jan-05 0:28
mr_mark_hewitt10-Jan-05 0:28 
GeneralRe: ie toolbar to block sites based on domain/ip address of the domain Pin
Martin Koorts10-Jan-05 3:49
Martin Koorts10-Jan-05 3:49 
GeneralRe: ie toolbar to block sites based on domain/ip address of the domain Pin
mr_mark_hewitt10-Jan-05 4:40
mr_mark_hewitt10-Jan-05 4:40 
GeneralRe: ie toolbar to block sites based on domain/ip address of the domain Pin
Martin Koorts10-Jan-05 5:27
Martin Koorts10-Jan-05 5:27 
Generalatomic clock and time zone programming Pin
mr_mark_hewitt9-Jan-05 20:32
mr_mark_hewitt9-Jan-05 20:32 
GeneralRe: atomic clock and time zone programming Pin
Martin Koorts9-Jan-05 23:04
Martin Koorts9-Jan-05 23:04 
GeneralDynamic Menu, WTL Pin
Johnny1239-Jan-05 19:41
Johnny1239-Jan-05 19:41 
GeneralChanging Button Style With WTL Pin
Johnny1238-Jan-05 20:28
Johnny1238-Jan-05 20:28 
GeneralRe: Changing Button Style With WTL Pin
Michael Dunn9-Jan-05 4:00
sitebuilderMichael Dunn9-Jan-05 4:00 
GeneralRe: Changing Button Style With WTL Pin
Johnny1239-Jan-05 13:40
Johnny1239-Jan-05 13:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.