Microsoft KB Archive/229904

= PRB: CAxWindow Members Can Cause a Memory Leak =

Article ID: 229904

Article Last Modified on 9/18/2003

-

APPLIES TO

 Microsoft ActiveX Template Library 3.0, when used with:  Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Professional Edition

 Microsoft Visual C++ 6.0 Standard Edition

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q229904

<div class="symptoms_section">

SYMPTOMS
The CAxWindow CreateControl and AttachControl functions may leak memory if not used correctly.

<div class="cause_section">

CAUSE
The following two techniques of creating ActiveX controls on composite controls (or any window supporting ATL containment) can result in memory leaks. //Intialize ATL control containment. AtlAxWinInit;

//Create container window. HWND hWndCont = m_ax.Create(m_hWnd, rect, 0, WS_CHILD | WS_VISIBLE);

//Create & activate ActiveX control HRESULT hr = m_ax.CreateControl("MSCAL.Calendar"); -OR- //Intialize ATL control containment. AtlAxWinInit; //Create container window HWND hWndCont = m_ax.Create(m_hWnd, rect, 0, WS_CHILD | WS_VISIBLE);

// Create ActiveX control. CComPtr<IUnknown> spunk; HRESULT hr = CLSIDFromProgID(OLESTR("MSCAL.Calendar"), &clsid); hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_IUnknown, (void**)&spunk);

// Activate ActiveX control. HRESULT hr = m_ax.AttachControl(spunk); In both techniques, the calls to Create results in the creation of a CAxHostWindow object. The call to CreateControl or AttachControl also creates another CAxHostWindow Object. Upon destruction, the CAxHostWindow object created by Create is freed.

<div class="resolution_section">

RESOLUTION
Use one of the techniques mentioned in article Q218442 to create ActiveX controls at run time.

<div class="status_section">

STATUS
This behavior is by design.

<div class="references_section">