From the example
http://msdn.microsoft.com/en-us/library/ee504258.aspx AddElementToVisualTree
- what is the correct way to remove the element?
The following code leaks memory and at the moment I'm trying to determine if
there is another step that I need to do, or if there's a problem with the
underlying objects.
I have an application based on the XAMLPERF code that adds and removes
buttons from a stack panel using code. It doesn't leak without this code
included.
The code below is triggered on a timer, the overall effect is to add 10
buttons, then removing them [this is just the part that adds or removes the
element from the collection.]
1 int index = 0;
2 IXRUIElementCollectionPtr children = NULL;
3 IXRButtonPtr button = NULL;
4 if (!FAILED(m_pStackPanel->GetChildren(&children)))
5 {
7 if (add)
8 {
9 if (!FAILED(m_pApp->CreateObject(&button)))
10 {
11 button->SetHeight(20);
13 WCHAR Text[MAX_PATH] = L"Button";
14 XRValue value;
15 value.SetNull();
16 value.vType = VTYPE_BSTR;
17 value.bstrStringVal = Text;
18 button->SetContent(&value);
19 hr = children->Add(button, &index);
20 if (FAILED(hr))
21 {
22 // Some error handling here
23 }
24 }
25 }
26 else
27 {
28 if (!FAILED(children->GetCount(&index)))
29 {
30 hr = children->RemoveAt(index-1);
31 if (FAILED(hr))
32 {
33 // Some error handling here
34 }
35 }
36 }
37 }
I have also noticed that if I addRef and Release to the button after it has
been added to the children collection it returns 4 (so would be ref counted
to 3 after the release, which is two more than before adding it to the
collection)
So at line 10 the refcount is 1, after line 19 it would be 3. I've tried
not using the template wrappers and releasing the objects myself, but that
performed similarly. Does anyone know how it should be done?
Thanks John