Commit d2b7bc13c67dcbacff090b0416292a1686d671b8

Authored by Zou XiKun
1 parent a74689b7

修正内存泄漏,崩潰問題

src/left_over/MSRegionSurveilanceCpu.cpp
... ... @@ -25,7 +25,8 @@ int IRegionSurveillanceCpu::rs_init(const rs_params &param)
25 25 {
26 26 int r = 0;
27 27 IplImage * gray = cvCreateImage(cvSize(param.image.w_, param.image.h_), 8, 1);
28   - cvSetData(gray, param.image.data_, param.image.w_);
  28 + memcpy(gray->imageData, param.image.data_, param.image.w_ * param.image.h_* param.image.c_);
  29 + //cvSetData(gray, param.image.data_, param.image.w_);
29 30 for (int i = 0; i < rect_num; ++i)
30 31 {
31 32 IplImage * img = cvCreateImage(cvSize(rect[i].width_, rect[i].height_), 8, 1);
... ... @@ -35,7 +36,7 @@ int IRegionSurveillanceCpu::rs_init(const rs_params &amp;param)
35 36 (unsigned char*)(img->imageData), 1/*param.image.c_*/, 1/*param.channel_deal*/, param.filt_flag, param.min_area, param.max_area);
36 37 cvReleaseImage(&img);
37 38 }
38   - //cvReleaseImage(&gray);
  39 + cvReleaseImage(&gray);
39 40 return r;
40 41 }
41 42 int IRegionSurveillanceCpu::rs_init_region(int num_roi, region_info* region_infos/*, bool iflog*/)
... ... @@ -87,8 +88,11 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &amp;img_data, region_info* regio
87 88  
88 89 int totalObjCount = 0;
89 90 int totalDelCount = 0;
  91 + bool toObjCountBreak = false;
  92 + bool toDelCountBreak = false;
90 93 IplImage * gray = cvCreateImage(cvSize(img_data.w_, img_data.h_), 8, 1);
91   - cvSetData(gray, img_data.data_, img_data.w_);
  94 + memcpy(gray->imageData, img_data.data_, img_data.w_*img_data.h_ * img_data.c_);
  95 + //cvSetData(gray, img_data.data_, img_data.w_);
92 96 for (int k = 0; k < rect_num; ++k)
93 97 //int k = 0;
94 98 {
... ... @@ -100,12 +104,13 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &amp;img_data, region_info* regio
100 104  
101 105 auto * ObjInfo = new MS_ObjectInfo[ObjCount];
102 106 IReginCpu[k]->getObjectInfo(ObjCount, ObjInfo);
103   - for (int i = 0; i < ObjCount; ++i)
  107 + for (int i = 0; i < ObjCount && !toObjCountBreak; ++i)
104 108 {
105 109 result->obj_infos[totalObjCount].curPos.x_ = ObjInfo[i].curPos.x+rect[k].left_; // 当前坐标
106 110 result->obj_infos[totalObjCount].curPos.y_ = ObjInfo[i].curPos.y+rect[k].top_; // 当前坐标
107 111  
108 112 result->obj_infos[totalObjCount].trace.trace_num = ObjInfo[i].trace.nTraceNum; // 轨迹及报警状态
  113 + printf("nTraceNum = %d\n", ObjInfo[i].trace.nTraceNum);
109 114 for (int j = 0; j < MAXTRACENUM; ++j)
110 115 {
111 116 result->obj_infos[totalObjCount].trace.obj_trace[j].x_ = ObjInfo[i].trace.pObjTrace[j].x+rect[k].left_;
... ... @@ -123,19 +128,26 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &amp;img_data, region_info* regio
123 128 result->obj_infos[totalObjCount].pb_alarm_type[j] = ObjInfo[i].pbAlarmType[j];
124 129 }
125 130 ++totalObjCount;
  131 + if (totalObjCount >= MACDETECTOBJNUM)
  132 + {
  133 + toObjCountBreak = true;
  134 + break;
  135 + }
126 136 }
127   -
  137 + cvReleaseImage(&img);
128 138 delete ObjInfo;
  139 +
129 140 auto delCount = IReginCpu[k]->getDeleteNum();
130 141  
131   - auto * DelInfo = new MS_ObjectInfo[delCount];
  142 + auto * DelInfo = new MS_ObjectInfo[delCount]{};
132 143 IReginCpu[k]->getDeleteInfo(delCount, DelInfo);
133   - for (int i = 0; i < delCount; ++i)
  144 + for (int i = 0; i < delCount && !toDelCountBreak; ++i)
134 145 {
135 146 result->del_infos[totalDelCount].curPos.x_ = DelInfo[i].curPos.x + rect[k].left_; // 当前坐标
136 147 result->del_infos[totalDelCount].curPos.y_ = DelInfo[i].curPos.y + rect[k].top_; // 当前坐标
137 148  
138 149 result->del_infos[totalDelCount].trace.trace_num = DelInfo[i].trace.nTraceNum; // 轨迹及报警状态
  150 + printf("del_infos nTraceNum = %d\n", DelInfo[i].trace.nTraceNum);
139 151 for (int j = 0; j < MAXTRACENUM; ++j)
140 152 {
141 153 result->del_infos[totalDelCount].trace.obj_trace[j].x_ = DelInfo[i].trace.pObjTrace[j].x + rect[k].left_;
... ... @@ -153,11 +165,17 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &amp;img_data, region_info* regio
153 165 result->del_infos[totalDelCount].pb_alarm_type[j] = DelInfo[i].pbAlarmType[j];
154 166 }
155 167 ++totalDelCount;
  168 + if (totalDelCount >= MACDETECTOBJNUM)
  169 + {
  170 + toDelCountBreak = true;
  171 + break;
  172 + }
156 173 }
157 174 delete DelInfo;
158 175 }
159 176 result->obj_count = totalObjCount;
160 177 result->del_count = totalDelCount;
  178 + cvReleaseImage(&gray);
161 179 return 1;
162 180 }
163 181 //int IRegionSurveillanceCpu::rs_get_arrowdir(const sy_point &p_roi0, const sy_point &p_roi1, const sy_point &dir)
... ...
src/left_over/RegionSurveillanceProcess.h
1 1 #pragma once
2 2 //#include "MSRegionSurveilance.h"
3 3 #include "left_over_det.h"
4   -#include <boost/thread.hpp>
  4 +//#include <boost/thread.hpp>
5 5 #include "head.h"
6 6 #include <atomic>
7 7 //#include "MSRegionSurveilanceVpt.h"
... ... @@ -43,7 +43,7 @@ public:
43 43 //int long_side;
44 44 //int short_side;
45 45 std::atomic<bool> thrd_run;
46   - boost::thread thrd;
  46 + //boost::thread thrd;
47 47 long frame_num = 0;
48 48 bool init_flag = false;
49 49 sy_rect rect[MAXVERTEXNUM]; //ÓÐÐ§ÇøÓò
... ...
src/left_over/ms_region_surveilance.cpp
... ... @@ -242,9 +242,12 @@ int rs_process(void *handle, sy_img img_data, rs_result *result)
242 242  
243 243 IplImage * gray = cvCreateImage(cvSize(srcscale->width, srcscale->height), 8, 1);
244 244 cvCvtColor(srcscale, gray, CV_RGB2GRAY);
  245 +
245 246 sy_img image;
  247 + unsigned char * data = (unsigned char *)malloc(gray->width*gray->height*gray->nChannels);
  248 + memcpy(data, gray->imageData, gray->width*gray->height*gray->nChannels);
246 249 //image.set_data(srcImage.cols, srcImage.rows, srcImage.channels(), (unsigned char *)srcImage.data);
247   - image.set_data(gray->width, gray->height, gray->nChannels, (unsigned char *)gray->imageData);
  250 + image.set_data(gray->width, gray->height, gray->nChannels, (unsigned char *)data);
248 251 if (regin->init_flag == false)
249 252 {
250 253 rs_params param;
... ... @@ -258,11 +261,14 @@ int rs_process(void *handle, sy_img img_data, rs_result *result)
258 261 param.min_area = 100;
259 262 param.max_area = 1000000;
260 263 regin->rs_init(param);
  264 +
261 265 regin->init_flag = true;
262 266 int ret = regin->rs_init_region(1, &pRegionInfo/*, false*/);
263   - }
264 267  
  268 + }
  269 +
265 270 int ret = regin->rs_detect(image, &pRegionInfo, result);
  271 +
266 272 for (int i = 0; i < result->obj_count; ++i)
267 273 {
268 274 result->obj_infos[i].curPos.x_ *= regin->scale;
... ... @@ -296,8 +302,10 @@ int rs_process(void *handle, sy_img img_data, rs_result *result)
296 302 result->del_infos[i].tar_box.left_ *= regin->scale;
297 303 result->del_infos[i].tar_box.top_ *= regin->scale;
298 304 }
  305 + free(data);
299 306 cvReleaseImage(&gray);
300 307 cvReleaseImage(&srcscale);
  308 + //cvReleaseImage(&gray);
301 309 //cvReleaseImage(&srcscale2);
302 310 return ret;
303 311 }
... ...