Answered by:
Passing C++/CX Objects

Question
-
I'm trying to figure out how to safely handle C++/CX objects when passing them as function parameters, and when returning them from functions, within C++. For example:
ref class Foo sealed : public Windows::UI::Xaml::Controls::Control { ... } class FooManager { public: int AddFoo(Foo^ fooInst); Foo^ GetFoo(int fooIndex); private: std::vector<Foo^> Foos; }
Is that code ok? Or, will AddFoo and/or GetFoo cause leaks due to messed up Foo reference counts?
Wednesday, November 28, 2012 5:43 PM
Answers
-
Generally speaking, referencing counting 'smart pointers' like Microsoft::WRL::ComPtr<>, std::shared_ptr, etc. increment the reference count whenever they are created, and decrement it when they are destroyed. This can result in some performance issues with temporaries and calling convention. One way to get around this is to pass the smart pointers into functions by 'const reference' which works because the calling code context already has a valid smart pointer. Alternatively, you can pass 'raw' pointers but it really depends on what you are doing with them inside the function.
Returning smart-pointers of some kind is probably the safest thing to do with ref-counted objects rather than returning raw pointers.
The C++/CX extension and the ^ operator means that the compiler understands more about what's happening and it can optimize out these additional temporaries and useless ref increments/decrements, so generally you don't need to use the extra 'const &' pattern.
- Edited by Chuck Walbourn - MSFTMicrosoft employee Wednesday, November 28, 2012 9:42 PM
- Marked as answer by Bob Flora Wednesday, November 28, 2012 11:20 PM
Wednesday, November 28, 2012 9:41 PM
All replies
-
I suggest that you use this instead:
Microsoft::WRL::ComPtr
Toni Petrina
My blog: Toni codes .NET
If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"Wednesday, November 28, 2012 7:15 PM -
The code looks okay, but of course will it leak or not, depends on the AddFoo & GetFoo implementation.Wednesday, November 28, 2012 9:02 PM
-
Generally speaking, referencing counting 'smart pointers' like Microsoft::WRL::ComPtr<>, std::shared_ptr, etc. increment the reference count whenever they are created, and decrement it when they are destroyed. This can result in some performance issues with temporaries and calling convention. One way to get around this is to pass the smart pointers into functions by 'const reference' which works because the calling code context already has a valid smart pointer. Alternatively, you can pass 'raw' pointers but it really depends on what you are doing with them inside the function.
Returning smart-pointers of some kind is probably the safest thing to do with ref-counted objects rather than returning raw pointers.
The C++/CX extension and the ^ operator means that the compiler understands more about what's happening and it can optimize out these additional temporaries and useless ref increments/decrements, so generally you don't need to use the extra 'const &' pattern.
- Edited by Chuck Walbourn - MSFTMicrosoft employee Wednesday, November 28, 2012 9:42 PM
- Marked as answer by Bob Flora Wednesday, November 28, 2012 11:20 PM
Wednesday, November 28, 2012 9:41 PM