0

I got following warning in my project (both Release and Debug mode):

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\concurrent_vector.h(1599): warning C4189: '_Array' : local variable is initialized but not referenced
      C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\concurrent_vector.h(1598) : while compiling class template member function 'void Concurrency::concurrent_vector<_Ty>::_Destroy_array(void *,Concurrency::concurrent_vector<_Ty>::size_type)'
      with
      [
          _Ty=Vector3i
      ]
      d:\Some_path\somefile.h(780) : see reference to class template instantiation 'Concurrency::concurrent_vector<_Ty>' being compiled
      with
      [
          _Ty=Vector3i
      ]

Where somefile.h is my file and line 780 has this code:

Concurrency::concurrent_vector<Vector3i> m_ovColors;

Vector3i is something like this:

template<typename T> class TVector3 {
public:
    T x, y, z;
}

typedef TVector3<int>           Vector3i;

Code around line 1598 in concurrent_vector.h is (line 1598 is just '{'):

template<typename _Ty, class _Ax>
void concurrent_vector<_Ty, _Ax>::_Destroy_array( void* _Begin, size_type _N ) 
{
    _Ty* _Array = static_cast<_Ty*>(_Begin);
    for( size_type _J=_N; _J>0; --_J )
        _Array[_J-1].~_Ty(); // destructors are supposed to not throw any exceptions
}

What could be a reason for this? This somefile.h when included in other projects does not issue that kind of warning.

Bo Persson
  • 86,087
  • 31
  • 138
  • 198
IgorStack
  • 656
  • 1
  • 6
  • 19

3 Answers3

3

The problem is that the code turns into this because of inlining:

_Ty* _Array = static_cast<_Ty*>(_Begin);
for( size_type _J=_N; _J>0; --_J )
    ; // destructor has no effect!

At this point, the compiler stops and checks that all initialized variables are used, and emits that warning.

(The code will then be optimized to an empty function:

; // variable is unused
; // loop has no effect

but by this point the warnings have already been emitted.)

GManNickG
  • 459,504
  • 50
  • 465
  • 534
1

Perhaps the compiler is optimizing out the loop but not the static_cast. Do you have assembler/source listing for the relevant part of your program?

My experience with MSVC STL is that if you compile with /W4 you get lots of false positives.

Steve Townsend
  • 51,210
  • 8
  • 87
  • 134
1

This approach works:

#pragma warning( disable : 4189 )
#include <concurrent_vector.h>
#pragma warning( default : 4189 )

Thanks all

IgorStack
  • 656
  • 1
  • 6
  • 19
  • Your question wasn't about disabling warnings, it was about explaining where they were coming from. In the future, put your *actual* question in your post. We could have told you this days ago if you had asked. – GManNickG Apr 25 '12 at 20:34