How to delete simple ATL object. 
Author Message
 How to delete simple ATL object.

Hi all.

Im developing ActiveX ATL control, based on SysListView32, and im little
bit confused
how to delete an object. My control acts bretty much like
mictrosoft ListView ocx, which means that i have a collection
of ATL objects, stored in CCimpleMap. Client adds a new
object by calling Add function and control returns new object
to the client (list ListVew OCX). For example:

STDMETHODIMP CATPropertyEditor::AddDate(
 /*[in                                            ]*/ BSTR          Name,
 /*[in , defaultvalue(ATShortDate)]*/ ATDateFormat  DateFormat,
 /*[in , optional                             ]*/ VARIANT       Date,
 /*[in , optional                             ]*/ VARIANT       Key,
 /*[out, retval                                ]*/ IATProperty** pVal)
{

// .....some parameter checking

// Create new object
 CComObject<CATProperty> *NewProp = NULL;
CComObject<CATProperty>::CreateInstance(&NewItem);

// Assign parameters.....

// External reference for client
NewItem->QueryInterface(IID_IATProperty,(LPVOID *)pVal);
(*pVal)->AddRef(); // Internal reference to keep object alive (refcount
sould be 2 now)

// Adds new object to the listview ...adds new object to CSimpleMap as well
 InternalSetItem(NewProp);

 return S_OK;

Quote:
}

The client can get the reference to the object by calling properties
SelectedProperty or Properties(Index).
The SelectedProperty is implemented like:

STDMETHODIMP CATPropertyEditor::get_SelectedProperty(IATProperty **pVal)
{
    CHECK_E_POINTER(pVal) // Check parameter
    *pVal = NULL;

    // Get selected object in listview
    CComObject<CATProperty> *Prop = InternalGetSelectedProp();

    if ( Prop ) {
        Prop->QueryInterface(IID_IATProperty,(LPVOID *)pVal) ; // increase
reference count
   }

   return S_OK;

Quote:
}

Properties(Index) is implemented same way.

The problem raises when client requests to destroy one item from list :

STDMETHODIMP CATPropertyEditor::RemoveProperty(VARIANT Key, VARIANT_BOOL
*pVal)
{
     CHECK_E_POINTER(pVal)
    *pVal = FALSE;

    if ( Key.vt == VT_I4 ) {
          if ( !InternalRemoveItem(Key.lVal - 1) ) {
              return E_FAIL;
          }
    }
    else if ( Key.vt == VT_BSTR ) { // Key
        CString iKey = Key.bstrVal;
        if ( !InternalRemoveItem(iKey) ) { // resolves key and calls
InternalRemoveItem(int Index) eventually
            return E_FAIL;
        }
    }
    else {
        return
Error(IDS_ERR_ITEMNOTFOUND,IID_IATPropertyEditor,E_INVALIDARG);
    }

    *pVal = TRUE;
    return S_OK;

Quote:
}

BOOL CATPropertyEditor::InternalRemoveItem(int Index)
{
    // Check index
     if ( Index < 0 || Index >= m_Items.GetSize() ) { return FALSE; }

     // Get Item
    CComObject<CATProperty> *pRef = (CComObject<CATProperty>
*)m_Items.GetValueAt(Index);
    if ( !pRef ) { return FALSE; }

    // remove pointer from collection
    m_Items.Remove(pRef->m_Key);

     // I THINK THIS LINE CAUSING PROBLEMS
    delete pRef;

     // Delete item from listview
     int iCount = ListView_GetItemCount(m_LView.m_hWnd);

    return TRUE;

Quote:
}

What i want to do is when client removes item i want to force to destroy the
object, so all reference from
client side becomes illegal. the "delete pRef" seems to work just fine if
the Key parameter is an item index.
But if i call RemoveProperty from VB like this:

ATPropertyEditor.RemoveProperty(ATPropertyEditor.SelectedProperty.Key);

the VB crashes.

Can anyone tell me what im doing wrong and what is the right way to do it ?

Thanx for advance...

--

-----------------------------------------------------

Asko Telinen

Faculty of Information technology
Jyv?skyl? university

-----------------------------------------------------



Tue, 29 Nov 2005 14:42:01 GMT  
 How to delete simple ATL object.

Quote:
> What i want to do is when client removes item i want to force to
destroy the
> object, so all reference from
> client side becomes illegal.

You can't do this. What you can do is set some flag in the object to
indicate that it is in invalid state. In every method, the object would
check the flag and report an error if it is set. You cannot delete the
object while any client has outstanding references to it - clients must
release all their referenes.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Tue, 29 Nov 2005 22:19:13 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Simple ATL Com object problem on simplest examples

2. Returns a ATL Simple object by a method of another ATLSimple object

3. Simple ATL Object-Returning a value to the calling object

4. ATL: Object deleted before window was destroyed

5. Changing/Deleting Methods in COM/ATL Objects

6. ATL simple object wizard

7. Simple ATL Object in my Windows Service

8. I need a HWND in a ATL Simple Object

9. ATL Simple Object in AcitveX Component

10. Cannot Create SIMPLE Atl object

11. Cannot Create SIMPLE Atl object

12. HOWTO set the property type of an ATL object to be another ATL object

 

 
Powered by phpBB® Forum Software