-1

I'm trying to build a library of cvblob using OpenCV, and while compiling in VS 2013,

    error C4703: potentially uninitialized local pointer variable 'track' used
    error C4703: potentially uninitialized local pointer variable 'blob' used

I'm not sure why this is, since the pointer variables are defined a block above (albeit in a separate loop). Here is the code:

  // Update track
  //cout << "Matching: track=" << track->id << ", blob=" << blob->label << endl;
  track->label = blob->label; // ERROR HERE
  track->centroid = blob->centroid;

I'm using pre-written header and source files, so I'm not sure what the problem is. Anyone know what the fix is?

Before //Update Track, Here is where 'track' and 'blob' are referenced above, with no errors:

  // Select track
  CvTrack *track;
  unsigned int area = 0;
  for (list<CvTrack*>::const_iterator it=tt.begin(); it!=tt.end(); ++it)
  {
    CvTrack *t = *it;

    unsigned int a = (t->maxx-t->minx)*(t->maxy-t->miny);
    if (a>area)
    {
      area = a;
      track = t;
    }
  }

  // Select blob
  CvBlob *blob;
  area = 0; 
solvador
  • 95
  • 1
  • 10
  • compiler compiles about blob not being initialized. if that's true, accessing blob->label or blob->centroid will lead to a segfault. are you missing some code initializing blob and track ? – berak Jun 20 '14 at 21:30
  • @berak I wouldn't think that there would be missing code, since these are developed, release codes for [cvBlob](https://code.google.com/p/cvblob/). I added some code to my original post above where both 'track' and 'blob' are referenced. – solvador Jun 20 '14 at 22:15
  • in your example above, track will only get initialized *if* it finds an area large enough. ( and again, you probably should avoid the deprecated c-api (and cvBlobslib) in general, they moved to c++ in 2010 already) – berak Jun 21 '14 at 07:50

2 Answers2

1

If the tt list is empty, or if it doesn't contain an element with the required area, then track will never be initialized. This is what the compiler complains about.

The function should probably only try to update the rack if it was actually found in the list of tracks.

The situation for blob is probably similar.

sth
  • 222,467
  • 53
  • 283
  • 367
  • Should be noted though that in some situations there is a premise known to the developer but not to the compiler, e.g. in this case it can be that the list isn't empty and contains the required element (it's probably not the best design but that's another subject). – SomeWittyUsername Jun 20 '14 at 23:23
1

You should avoid declaring pointers without giving them a value. Hence you should replace the declaration with

CvTrack *track=NULL; (if you use C++ without the latest version) or
CvTrack *track=nullptr; (with C++11)

With this you're sure you will always give a value to your pointer (even if it's NULL). Then you should check before assigning anything if the pointer is NULL to avoid a runtime error.

meneldal
  • 1,717
  • 1
  • 21
  • 30
  • This actually solved the issue for both blob and track. After a few frustrating hours the program works perfectly. – solvador Jun 24 '14 at 21:52