-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When to call AddRef and Release, and when only Release? #2133
Comments
Generally, the rules are:
To cover your specific examples, the first is an example of (1) & (3). The input parameter
Your second example shows (2). The method calls that provide a COM object as an out parameter to the caller, the method does an
|
Thank you for the elaborated response. It is clear to me now (and it confirms what I thought). |
Hi,
Although the sample code uses wrappers around the com objects to handle the com object lifetimes, I am interested to know when you should call both AddRef and Release, and when you should only call Release?
For example, in the WebView2APISample project, in AppWindow.cpp, storing the environment into m_webViewEnvironment will call AddRef() internally when setting, and Release when going out of scope:
However, for example when getting content/headers from a request (in ScenarioWebViewEventMonitor.cpp) , I don't think AddRef is called, because operator& is used to retrieve a pointer to the internal member of wil::com_ptr. So I assume that AddRef is not needed, and you should only call Release for the content/headers.
Is my assumption correct? And if yes/no: how can I know when you should call both AddRef and Release, and when should you call only Release?
Currently I assume that AddRef and Release are both needed in a callback function when the ICore... object is an argument, and you only need to call Release when you retrieve the pointer via a get_... (ICore... **) method.
Thanks,
Koen
The text was updated successfully, but these errors were encountered: