Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6564557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:58:55+00:00 2026-05-25T13:58:55+00:00

If I have an IUnknown *ptr , do I need to call Release() on

  • 0

If I have an IUnknown *ptr, do I need to call Release() on every interface I obtain through ptr->QueryInterface(), in addition to calling ptr->Release() when I’m done with ptr?

I used to think that the answer is “Yes”, but this quote from MSDN confused me:

Occasionally you may need to obtain a weak reference to an object (that is, you may wish to obtain a pointer to one of its interfaces without incrementing the reference count), but it is not acceptable to do this by calling QueryInterface followed by Release.

I don’t understand why that’s problematic — if I call ptr->QueryInterface() and then call Release on the resulting pointer, shouldn’t the reference count on the object still be positive? How does that result in an invalid pointer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:58:56+00:00Added an answer on May 25, 2026 at 1:58 pm

    The documentation is correct. And you need to follow reference counting rules – that includes calling Release on interfaces obtained from QueryInterface in addition to after you created the object.

    To clear up why you can’t do weak pointers with Release – there exists a race condition in calling QueryInterface and then Release immediately after.

    • Thread1 creates object – reference count 1
    • Thread2 calls QueryInterface for weak reference – reference count 2
    • Thread1 releases object – reference count 1
    • Thread2 calls Release for weak reference – reference count 0. Object is destroyed.
    • Thread2 tries to use object – error.

    The warning is there to guard against the above – presumably some programmers think that they can “call ptr->QueryInterface() and then call Release on the resulting pointer” and then use the object…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a class that implements the IUnknown interface. I have
I have a function in an IUnknown -derived COM interface: HRESULT GetPassword( [in] long
I have a C++ program calling an interface I've implemented in C#. I'm consistently
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
I have this basic setup: enum{ BASE, PRIMITIVE, ... }; class IUnknown{ public: bool
I got confused with __vfptr which I see through IDispatch/IUnknown pointers. I'm creating in-proc
We have a COM component who’s implementation and interface definition exist in managed code
I have a piece of code that uses Microsoft-specific extension to the C++: interface
From examples I've seen COM IUnknown::Release() function implementation is something like that: ULONG Release()
In COM when I have a well-known interface that I can't change: interface IWellKnownInterface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.