Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam

Webinar: Parsing C++ - 10.10

>
>
>
V1114. Suspicious use of 'dynamic_cast'…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

V1114. Suspicious use of 'dynamic_cast' when working with COM interfaces. Consider using the 'QueryInterface' member function.

Sep 27 2024

The analyzer has detected a suspicious use of the 'dynamic_cast' operator when working with COM interfaces. This can result in scenarios where the object reference counter is not incremented, leading to the handling of destroyed objects or other issues.

Let's examine the synthetic example. The code contains the COM interfaces, 'IDraw' and 'IShape', responsible for handling a certain geometric shape:

interface IDraw : public IUnknown
{
  ....
  virtual HRESULT Draw() = 0;
  ....
};

interface IShape : public IUnknown
{
  ....
  virtual HRESULT GetArea(double *area) = 0;
  ....
};

There is also the 'Circle' COM object that implements the 'IDraw' and 'IShape' interfaces:

class Circle : public IDraw, public IShape
{
  ....
};

Look at the example of the incorrect COM object handling via the 'IDraw' interface:

void foo(IDraw *ptrIDraw)
{
  IShape *ptrIShape = dynamic_cast<IShape*>(ptrIDraw);
  ....
  if (ptrIShape) 
    ptrIShape->GetArea(area);
  ....
}

In the above example, the reference counter for the 'Circle'-typed object does not increment. To increment and decrement the counter, call the 'AddRef' and 'Release' functions respectively.

To work with COM interfaces correctly, use the 'QueryInterface' function, specifically designed for this purpose.

The 'QueryInterface' function should:

  • check that the requested interface is accessible;
  • return a pointer to the requested interface;
  • increment the object reference counter.

Here is the fixed code:

void foo(IDraw *ptrIDraw)
{
  IShape *ptrIShape = nullptr;
  ....
  if (SUCCEEDED(ptrIDraw->QueryInterface(IID_IShape, &ptrIShape))
  ....
}