Boost logo

Boost :

From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2004-07-30 08:18:17


Andrew,

(I'm top posting since for some reason auto '>' for your original text is
defeated with this particular message)

One issue you mention is lifetime. I assume you have a either a CListCtrl or
CListView derived class. This class should have it's own vector<shared_ptr>.
Then the lifetime of the listview items is ensured.

No you can NOT use a casted shared_ptr in this fashion. You can
reinterpret_cast<DWORD_PTR>(&*my_shared_ptr) if you feel you must. This will
NOT increment any reference count, and as you found is not safe.

The CListCrtrl primarily traffics in indices(LV_ITEM::item), so I generally
forego the item data and just access vector<shared_ptr<>>[LV_ITEM::item]
directly. In fact, specifying LVS_OWNERDATA style, I get much better
performance and less memory usage than using the set item data approaches.

-----------------
Jeff Flinn
Applied Dynamics, International

"Andrew Shiels" <aeshiels_at_[hidden]> wrote in message
news:005301c475af$c2fb9250$08b15651_at_andy...
Im very new to boost, and have just started to intergrate the shared_ptr
smart pointer into one of my applications. I am storing a lot of pointers
(to objects) in a std::vector.

typedef boost::shared_ptr<CMyClass> my_shared_ptr;
typedef std::vector<my_shared_ptr> my_shared_ptr_vector;

What i need to do is to additionally store a reference to the same objects
(in the vector) in a CListCtrl (MFC). The CListCtrl allows you to associate
a pointer with each item (row) in the list control.

Before i used shared_ptr i used to add the data as below:

CMyClass pItem = new CMyClass();
my_vector->push_back(pItem);
CListCtrl::SetItemData->SetItemData(iItemNumber, (DWORD)pItem);

Now using shared_ptr (see below) i get a compiler error saying that it
cannot convert from my_shared_ptr to DWORD.

my_shared_ptr myPtr (new CMyClass();
my_shared_ptr_vector->push_back(myPtr);
CListCtrl::SetItemData->SetItemData(iItemNumber,
reinterpret_cast<DWORD_PTR>(myPtr));

My second question is if i was able to add the item data as ...

CListCtrl::SetItemData->SetItemData(iItemNumber,
reinterpret_cast<DWORD_PTR>(myPtr));

      ...would the internal reference counter be incremented in shared_ptr?

 My vector may be destroyed before my CListCtrl and I dont want the item
data to be deleted until both are out of scope.

Thanks

Andy

_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk