Crash using std::map to sort items in ObservableCollection
Posted: 17 Sep 2021, 10:51
I'm using an std::map to sort an ObservableCollection.
The type I'm containing is defined like this:
My ObservableCollection is declared like this:
And I am populating it like this:
I had trouble sorting the ObservableCollection directly; it seemed to cause the association ListBox to fall over (I was doing a crude bubble sort so items were being swapped a lot).
So I decided to use an std::map and std::vector (for duplicate keys) like this:
It seems to crash when I do this:
Here:
I *was* populating it like this:
And that worked fine, but this caused a memory leak, and it seemed proper to use MakePtr. Must admit, I haven't quite got my head around Noesis's ref counted pointers, they seem very different to std smart pointers!
What is the proper way of doing this?
Thanks.
The type I'm containing is defined like this:
Code: Select all
class MyType : public NoesisApp::NotifyPropertyChangedBase
Code: Select all
Noesis::Ptr<Noesis::ObservableCollection<MyType>> myCollection;
Code: Select all
myCollection->Add(Noesis::MakePtr<MyType>(item));
So I decided to use an std::map and std::vector (for duplicate keys) like this:
Code: Select all
std::map<std::string, std::vector<MyType*>> sortedItems;
// Populate the std::map using the sort key as the key, and storing the item as the value.
for (int i = 0; i < itemCount; ++i) {
auto key = GetSortKey(i, itemIndex);
auto findIt = sortedItems.find(key);
if (findIt != sortedItems.end()) {
findIt->second.push_back(myCollection->Get(i));
} else {
sortedItems[key].push_back(myCollection->Get(i));
}
}
auto currentSelectedItem = GetSelectedItem();
myCollection->Clear();
if (sortDescending) {
for (auto it = sortedItems.rbegin(); it != sortedItems.rend(); ++it) {
for (auto item : it->second) {
myCollection->Add(item);
}
}
} else {
for (auto it = sortedItems.begin(); it != sortedItems.end(); ++it) {
for (auto item : it->second) {
myCollection->Add(item);
}
}
}
Code: Select all
myCollection->Add(item);
Code: Select all
void* Noesis::Cast(const TypeClass* destType, BaseObject* source)
{
NS_ASSERT(destType != 0);
if (source != 0)
{
const TypeClass* typeClass = source->GetClassType(); <====== READ ACCESS VIOLATION
Code: Select all
myCollection->Add(new MyType(item));
What is the proper way of doing this?
Thanks.