18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

VISUAL C++<br />

// Create dialog class on the heap using ATL CComObject template.<br />

CComObject *myDlg;<br />

CComObject::CreateInstance(&myDlg);<br />

myDlg->AddRef(); // Keep dialog box alive until you're done with it.<br />

myDlg->DoModal(); // Launch the dialog box; when method returns, dialog<br />

box has exited.<br />

myDlg->Release(); // Typically, the refcount now goes to 0 and frees the<br />

dialog object.<br />

• Implement an intermediate COM object for use by the sink; this is sometimes<br />

called a listener or event helper object. This object typically contains no implementation<br />

but simply uses C++ method calls to forward events to the sink<br />

object. The listener has its reference count incremented by the source, but the<br />

sink’s reference count is unaffected. This breaks the cycle, allowing the sink’s<br />

reference count to reach 0 when all other references are released. As the sink<br />

executes its destructor code, it instructs the listener to unadvise and release<br />

the source.<br />

An alternative to using C++ pointers to communicate between listener and sink<br />

is to use an interface pointer that is a weak reference. That is, the listener contains<br />

a COM pointer to the sink but does not increment the sink’s reference<br />

count. It is the responsibility of the sink to ensure that this pointer is not accessed<br />

after the sink object has been released.<br />

140 • <strong>ArcGIS</strong> <strong>Engine</strong> <strong>Developer</strong> <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!