Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IXRUIElementCollection Memory Leak when removing elements?

Skip to first unread message

JohnDr

unread,
Dec 9, 2009, 9:46:01 AM12/9/09
to
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

JohnDr

unread,
Dec 10, 2009, 5:39:02 AM12/10/09
to
I've done some further investigation and the leak only happens when I add it
into the Children collection.

The reference count behaves as expected from 1 when the button is created,
to 3 when it is added into the children collection.

When removing - GetItem populates the IXRButtonPtr (ref Count 3), RemoveAt
reduces the number to 1, so I am expecting the Smart Pointer to finish and
tidy up the object at the end.

Is this is bug or is there something else that I need to do with the smart
pointer or collection?
Thanks

JohnDr

0 new messages