Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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.
Posted
Updated 13-Feb-10 20:15pm
v8

1 solution

Brian Bennett wrote:
Is it bad to have a member variable that's a reference?

No.

Brian Bennett wrote:
Can one later assign to that variable?

No.

Brian Bennett wrote:
Would it be bad for the reference member if something else were assigned to the original pointer?

It might be bad (i.e. the odds are quite high).

Brian Bennett wrote:
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?

I suppose you should post the relevent code. It may be bad or fine, English description may be ambiguous about.


Brian Bennett wrote:
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;

}


It looks clumsy.
Why do you call sort passing two iterators of copyList and a *this reference (that's looks really odd)?
Moreover what is the purpose of all that assignments (i.e. myList to copyList and then, on return, copyList to myList)? You may define a method acting directly on myList reference (less general, but better performance).

:)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900