I've been trying to track down a leak for the past few days. I'm not using any special tools, just closing the app and using the output window in .net. While going through the code these questions arose.
Is it bad to have a member variable that's a reference? Can one later assign to that variable?
Would it be bad for the reference member if something else were assigned to the original pointer?
Also, there's a function that passes a vector reference and assigns it to a local instance, performs a sort on the instance, then returns the instance, which is assigned to the original reference. This can't be right can it?
typedef std::vector< strong_ptr< CMyItem > > MyList;
void MyClass::AddItem( MyList & myList, CMyItem * myItem )
{
myList.push_back( myItem );
myList = SpecialSort( myList );
}
MyList SortClass::SpecialSort( MyList & myList )
{
MyList copyList;
copyList = myList;
sort( copyList.begin(), copyList.end(), *this );
return copyList;
}
Finally, is it possible for the output window (.net) to show misleading information about what is actually leaking? Could something else be causing a leak that's not what's reported? Or is it true that if the output shows it as leaking, that thing is guaranteed to be a culprit?
Thanks.
Response to Answer 1:
Thanks, cPallini.
The sort with *this is a functor thing.
If I were were writing that code today, I would probably simply alter the passed ref and not even have the function return anything. Making a local variable and assigning the parameter was a technique that an engineer taught me as a good practice, but maybe that doesn't make sense with refs.
But seeing that old code there made me wonder why it was working. As I am a self-taught programmer, there are many little holes of knowledge I would like to fill.
But your answer seems to infer that the code should work but I'm surprised because a copy/instance is assigned to the reference.
What I'd really like to know is if any if the issues I mentioned could cause a leak.