Project

General

Profile

Revision 513

fixed memory leak in vision code

View differences:

trunk/code/projects/colonet/server/includes/PositionMonitor.h
14 14
using namespace std;
15 15

  
16 16
#define MAX_POSITIONS 20
17
#define MAX_DISTANCE 5
17
#define SAME_ROBOT_DISTANCE_THRESHOLD 10
18 18

  
19 19
using namespace std;
20 20

  
......
24 24
  ~PositionMonitor(void);
25 25

  
26 26
  void run(void);
27
  int stopMonitoring(void);
27
  //int stopMonitoring(void);
28 28
  int updatePositions(void);
29
  
29

  
30 30
  int assignRealId(int old_id, int real_id);
31 31
  map<int, VisionPosition> getAllRobotPositions(void);
32 32
  int getRobotPosition(int robot_id, int* xbuf, int* ybuf);
trunk/code/projects/colonet/server/vision/vision.c
23 23
  int count;
24 24
};
25 25

  
26
static IplImage *image02, *image03, *image04;
27 26
static char* filename;
28 27

  
29 28
int vision_init(char* filename_) {
......
32 31
}
33 32

  
34 33
int vision_get_robot_positions(VisionPosition** positions) {
35
  CvMemStorage* stor;
36
  CvSeq* cont;
37
  CvBox2D32f* box;
38
  CvPoint* PointArray;
39
  CvPoint2D32f* PointArray2D32f;
34
  IplImage* image03;
40 35

  
41
  if ((image03 = cvLoadImage(filename, 0)) == 0) {
42
    fprintf(stderr, "Failed to load image.\n");
43
    return -1;
44
  }
45

  
46 36
  // load image and force it to be grayscale
47 37
  if ((image03 = cvLoadImage(filename, 0)) == 0) {
48 38
    fprintf(stderr, "Failed to load image.\n");
......
50 40
  }
51 41

  
52 42
  // Create the destination images
53
  image02 = cvCloneImage(image03);
54
  image04 = cvCloneImage(image03);
43
  IplImage* image02 = cvCloneImage(image03);
44
  IplImage* image04 = cvCloneImage(image03);
55 45

  
56 46
  if (DEBUG) cvNamedWindow("Result", 1);
57 47

  
58 48
  // Create dynamic structure and sequence.
59
  stor = cvCreateMemStorage(0);
60
  cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
49
  CvMemStorage* stor = cvCreateMemStorage(0);
50
  CvSeq* cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
61 51

  
62 52
  struct CenterP bestc[100];
63 53
  int index=0;
64 54

  
65
  int h;
66
  for (h = MINH; h < MAXH; h++) {
55
  for (int h = MINH; h < MAXH; h++) {
67 56
    // Threshold the source image. This needful for cvFindContours().
68 57
    cvThreshold(image03, image02, h, 255, CV_THRESH_BINARY);
69 58

  
70 59
    // Find all contours.
71
    cvFindContours( image02, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
60
    cvFindContours(image02, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
72 61

  
73 62
    // Clear images. IPL use.
74 63
    cvZero(image02);
......
85 74
      if (count < 6) continue;
86 75

  
87 76
      // Alloc memory for contour point set.
88
      PointArray = (CvPoint*) malloc(count * sizeof(CvPoint));
89
      PointArray2D32f= (CvPoint2D32f*) malloc(count * sizeof(CvPoint2D32f));
77
      CvPoint* PointArray = (CvPoint*) malloc(count * sizeof(CvPoint));
78
      CvPoint2D32f* PointArray2D32f= (CvPoint2D32f*) malloc(count * sizeof(CvPoint2D32f));
90 79

  
91 80
      // Alloc memory for ellipse data.
92
      box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
81
      CvBox2D32f* box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
93 82

  
94 83
      // Get contour point set.
95 84
      cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
......
175 164
  // Show image. HighGUI use.
176 165
  if (DEBUG) cvShowImage( "Result", image04 );
177 166

  
167
  cvReleaseImage(&image04);
168

  
169
  cvReleaseMemStorage(&stor);
170

  
178 171
  *positions = pos_array;
179 172
  return count;
180 173
}
trunk/code/projects/colonet/server/vision/Makefile
4 4

  
5 5
all: clean_programs clean_vision vision vision_driver fitellipse
6 6

  
7
vision: 
7
vision:
8 8
	$(CC) -ggdb `pkg-config opencv --cflags --libs` -c -I . vision.c
9 9

  
10 10
vision_driver: vision
trunk/code/projects/colonet/server/PositionMonitor.cpp
36 36
  }
37 37
}
38 38

  
39
int PositionMonitor::stopMonitoring() {
40
  //TODO: fill this in when this becomes asynchronous
41
  return 0;
42
}
43

  
44 39
int PositionMonitor::updatePositions() {
45 40
  VisionPosition * positions = NULL;
46 41

  
......
126 121

  
127 122
int PositionMonitor::getRobotPosition(int robot_id, int* xbuf, int* ybuf) {
128 123
  //TODO: figure out what a map returns if the element doesn't exist
129
  
124

  
130 125
  pthread_mutex_lock(&position_map_lock);
131 126

  
132
  
133 127
  if (positionMap.find(robot_id) == positionMap.end()){
134 128
    return -1;
135 129
  }
......
147 141
bool PositionMonitor::isProbablySameRobot(VisionPosition p1, VisionPosition p2) {
148 142
  int xDiff = p1.x - p2.x;
149 143
  int yDiff = p1.y - p2.y;
150
  return (xDiff*xDiff + yDiff*yDiff < MAX_DISTANCE*MAX_DISTANCE);
144
  return (xDiff*xDiff + yDiff*yDiff < SAME_ROBOT_DISTANCE_THRESHOLD*SAME_ROBOT_DISTANCE_THRESHOLD);
151 145
}

Also available in: Unified diff