In the 1st call to getQuerySetQueries() from getAllQuerySetQueries() , querySetTree is passed in as the root of the connection..
So it looks like you're altering Dali tree elements here.. 

I think you want:
> IPropertyTree *queryWithSetId = queryTreeRoot->addPropTree("Query", LINK(&query));
> queryWithSetId->addProp("@querySetId", querySetId);

the addPropTree will have resulted in the added tree being cloned and returned for you to alter.