0

I'm currently working on a kinect project in C++. I'm working under Ubuntu 12.04 (32 bit) and I use OpenNI/NITE. I want to find a hand and save its coordinates in a list and for this part I have the following code:

Header:

#ifndef HAND_H_
#define HAND_H_
#include <list>
#include<iostream>
#include <stdio.h>

class Hand
{
public:
    std::list<double> Xpoints;
    std::list<double> Ypoints;
    std::list<double> Zpoints;
    Hand(int id);
    int handID;
    void AddPoint(double x, double y, double z);
    void ClearList(void);
    void ReleaseHand(void);
    int GetID(void);
    int Size();
};
#endif

CPP:

#include "Hand.h"

Hand::Hand(int id)
{
    handID = id;
}


void Hand::AddPoint(double x, double y, double z)
{
    ("Hand ID: %d: (%f,%f,%f)\n", handID, x, y, z);
    if(handID > 0)
    {
        Xpoints.push_back(x);
        Ypoints.push_back(y);
        Zpoints.push_back(z);
        ("Added to Hand ID: %d: (%f,%f,%f)\n", handID, x, y, z);
    }
    else
    {
        std::cout << "Hand does not exist anymore - Inconsistency!" << std::endl;
    }
}


void Hand::ClearList(void)
{
    Xpoints.clear();
    Ypoints.clear();
    Zpoints.clear();
}

void Hand::ReleaseHand(void)
{
    handID = -1; // if (ID==-1) no valid hand
    Xpoints.clear();
    Ypoints.clear();
    Zpoints.clear();
}

int Hand::GetID(void)

{
    return handID;
}

int Hand::Size()
{
    return Xpoints.size();
}

Here AddPoints() gets called:

Hand hand(cxt->nID);
hand.AddPoint(cxt->ptPosition.X, cxt->ptPosition.Y, cxt->ptPosition.Z);
handlist.Add(&hand);

and it works fine. Later, when I get a new coordinate for the hand from my kinect I call this:

Hand tmp = handlist.Get(cxt->nID);
tmp.AddPoint(cxt->ptPosition.X, cxt->ptPosition.Y, cxt->ptPosition.Z);
std::cout << "Hand size " << tmp.Size() << std::endl;

Here, when this is called the first time a second set of coordinates is added to the list in my hand. But after that the lists can't get bigger than size 2. Which means instead of inserting, the last points are replaced. (I've got them printed, so I could see the coordinates) I've also tried push_front which replaces coordinates in the front, as well as vectors instead of lists which has the same problem for push_back and insert. I've also tried the same code in Windows with VS2010 where it worked fine. I have absolutely no idea what I'm doing wrong.

So it would be great if someone could help me. :)

Omnifarious
  • 54,333
  • 19
  • 131
  • 194

1 Answers1

2

I see a couple problems.

One you're adding the pointer to a stack variable to the handlist. As soon as the hand goes out of scope, you have a dangling pointer.

Second, your handlist.Get is returning (presumably) by copy and thus anything you add to it isn't reflected in the version that handlist occupies. ie: You could call handlist.Get(), add 500 points, then call handlist.Get again and it would be the original version.

Your handlist.Get() function is also not returning a pointer, but you're passing handlist.Add() a pointer so your interface isn't clear.

Ryan Guthrie
  • 688
  • 3
  • 11