[vtk-developers] vtkSmartPointer and gcc -Wunused-variable

Moreland, Kenneth kmorel at sandia.gov
Mon Nov 29 13:03:00 EST 2010


GCC is not warning about an unused variable because your declaration includes lots of semantic sugar to hide actual code that gets executed.  Specifically, the custom constructor and destructor of the smart pointer are called, and the compiler has no way to know that the constructor and destructor does not have important global effects.  In fact, there are times where the smart pointer may be used almost exactly like this where it has important side effects that you would not want the compiler to complain about.  For example, consider this small change to your code:

vtkPolyData *pd = vtkPolyData::New();
vtkSmartPointer<vtkPolyData> polydata = pd;
pd->Delete();

As far as the compiler is concerned, not much has changed; a smart pointer is constructed with a regular pointer.  But this time there is a very important side effect in that the operations in the constructor are preventing the object from being deleted until the smart pointer leaves scope and its destructor is called.  Because there is no reliable way for the compiler to determine the usefulness of the constructor and destructor, it always assumes that they do something important.

-Ken


On 11/24/10 2:50 PM, "Sean McBride" <sean at rogue-research.com> wrote:

Hi all,

Consider this simple code:

-------
#include "vtkSmartPointer.h"
#include "vtkPolyData.h"

int main (int argc, char *argv[])
{
        (void)argc; (void)argv;

        int a = 0;
        vtkSmartPointer<vtkPolyData> polydata = 0;

        return 0;
}
-------

Then:

$ gcc -Wunused-variable -I/vtk/include/vtk-5.7 test.cp

test.cp: In function 'int main(int, char**)':
test.cp:8: warning: unused variable 'a'

Notice that gcc does *not* warn that "polydata" is unused, but does for
"a".  I'm curious why that is, and if anyone knows of a way to get gcc
to warn?  (clang also does not warn.)

Cheers,

--
____________________________________________________________
Sean McBride, B. Eng                 sean at rogue-research.com
Rogue Research                        www.rogue-research.com
Mac Software Developer              Montréal, Québec, Canada
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtk-developers




   ****      Kenneth Moreland
    ***      Sandia National Laboratories
***********
*** *** ***  email: kmorel at sandia.gov
**  ***  **  phone: (505) 844-8919
    ***      web:   http://www.cs.unm.edu/~kmorel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20101129/f9907a84/attachment.html>


More information about the vtk-developers mailing list