opencv_trail_effect.cpp: fix potential leak when exiting during option parsing
[experiments/opencv_trail_effect.git] / opencv_trail_effect.cpp
index 5fec5dc..bc5b187 100644 (file)
@@ -71,13 +71,13 @@ int main(int argc, char *argv[])
        int ret = 0;
        int opt;
 
        int ret = 0;
        int opt;
 
-       std::string *input_file = NULL;
-       std::string *output_file = NULL;
+       std::string input_file;
+       std::string output_file;
        int trail_lenght = 25;
        int trail_lenght = 25;
-       std::string *segmentation_method = new std::string("background");
+       std::string segmentation_method("background");
        int background_learn_frames = 50;
        int threshold_level = 5;
        int background_learn_frames = 50;
        int threshold_level = 5;
-       std::string *drawing_method = new std::string("copy");
+       std::string drawing_method("copy");
        bool reverse_trail = false;
        bool show_background = false;
        bool redraw_current_frame = false;
        bool reverse_trail = false;
        bool show_background = false;
        bool redraw_current_frame = false;
@@ -85,17 +85,16 @@ int main(int argc, char *argv[])
        while ((opt = getopt(argc, argv, "i:o:l:s:b:t:d:rBFh")) != -1) {
                switch (opt) {
                case 'i':
        while ((opt = getopt(argc, argv, "i:o:l:s:b:t:d:rBFh")) != -1) {
                switch (opt) {
                case 'i':
-                       input_file = new std::string(optarg);
+                       input_file = std::string(optarg);
                        break;
                case 'o':
                        break;
                case 'o':
-                       output_file = new std::string(optarg);
+                       output_file = std::string(optarg);
                        break;
                case 'l':
                        trail_lenght = atoi(optarg);
                        break;
                case 's':
                        break;
                case 'l':
                        trail_lenght = atoi(optarg);
                        break;
                case 's':
-                       delete segmentation_method;
-                       segmentation_method = new std::string(optarg);
+                       segmentation_method = std::string(optarg);
                        break;
                case 'b':
                        background_learn_frames = atoi(optarg);
                        break;
                case 'b':
                        background_learn_frames = atoi(optarg);
@@ -104,8 +103,7 @@ int main(int argc, char *argv[])
                        threshold_level = atoi(optarg);
                        break;
                case 'd':
                        threshold_level = atoi(optarg);
                        break;
                case 'd':
-                       delete drawing_method;
-                       drawing_method = new std::string(optarg);
+                       drawing_method = std::string(optarg);
                        break;
                case 'r':
                        reverse_trail = true;
                        break;
                case 'r':
                        reverse_trail = true;
@@ -130,8 +128,8 @@ int main(int argc, char *argv[])
        cv::Size frame_size;
        cv::Mat input_frame;
 
        cv::Size frame_size;
        cv::Mat input_frame;
 
-       if (input_file) {
-               inputVideo.open(*input_file);
+       if (!input_file.empty()) {
+               inputVideo.open(input_file);
        } else {
                inputVideo.open(0);
        }
        } else {
                inputVideo.open(0);
        }
@@ -142,15 +140,15 @@ int main(int argc, char *argv[])
                goto out;
        }
 
                goto out;
        }
 
-       frame_size = cv::Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),
-                             (int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));
+       frame_size = cv::Size((int) inputVideo.get(cv::CAP_PROP_FRAME_WIDTH),
+                             (int) inputVideo.get(cv::CAP_PROP_FRAME_HEIGHT));
 
 
-       if (output_file) {
-               int fps = inputVideo.get(CV_CAP_PROP_FPS);
+       if (!output_file.empty()) {
+               int fps = inputVideo.get(cv::CAP_PROP_FPS);
                if (fps < 0)
                        fps = 25;
 
                if (fps < 0)
                        fps = 25;
 
-               outputVideo.open(*output_file, CV_FOURCC('M','J','P','G'), fps, frame_size, true);
+               outputVideo.open(output_file, cv::VideoWriter::fourcc('M','J','P','G'), fps, frame_size, true);
                if (!outputVideo.isOpened()) {
                        std::cerr  << "Could not open the output video for write." << std::endl;
                        ret = -1;
                if (!outputVideo.isOpened()) {
                        std::cerr  << "Could not open the output video for write." << std::endl;
                        ret = -1;
@@ -166,24 +164,24 @@ int main(int argc, char *argv[])
 
        trail->setRedrawCurrentFrame(redraw_current_frame);
 
 
        trail->setRedrawCurrentFrame(redraw_current_frame);
 
-       if (trail->setDrawingMethod(*drawing_method) < 0) {
+       if (trail->setDrawingMethod(drawing_method) < 0) {
                std::cerr  << "Invalid drawing method." << std::endl;
                ret = -1;
                goto out_delete_trail;
        }
 
        Segmentation *segmentation;
                std::cerr  << "Invalid drawing method." << std::endl;
                ret = -1;
                goto out_delete_trail;
        }
 
        Segmentation *segmentation;
-       if (*segmentation_method == "background") {
+       if (segmentation_method == "background") {
                segmentation = new MOG2Segmentation(inputVideo, background_learn_frames);
                if (show_background) {
                segmentation = new MOG2Segmentation(inputVideo, background_learn_frames);
                if (show_background) {
-                       cv::Mat background(frame_size, inputVideo.get(CV_CAP_PROP_FORMAT));
+                       cv::Mat background(frame_size, inputVideo.get(cv::CAP_PROP_FORMAT));
 
                        ((MOG2Segmentation *)segmentation)->getBackgroundImage(background);
                        trail->setBackground(background);
                }
 
                        ((MOG2Segmentation *)segmentation)->getBackgroundImage(background);
                        trail->setBackground(background);
                }
-       } else if (*segmentation_method == "threshold") {
+       } else if (segmentation_method == "threshold") {
                segmentation = new ThresholdSegmentation(threshold_level);
                segmentation = new ThresholdSegmentation(threshold_level);
-       } else if (*segmentation_method == "none") {
+       } else if (segmentation_method == "none") {
                segmentation = new DummySegmentation();
        } else {
                std::cerr  << "Invalid segmentation method." << std::endl;
                segmentation = new DummySegmentation();
        } else {
                std::cerr  << "Invalid segmentation method." << std::endl;
@@ -191,7 +189,7 @@ int main(int argc, char *argv[])
                goto out_delete_trail;
        }
 
                goto out_delete_trail;
        }
 
-       cv::namedWindow("Frame", CV_WINDOW_AUTOSIZE);
+       cv::namedWindow("Frame", cv::WINDOW_NORMAL);
 
        for (;;) {
                inputVideo >> input_frame;
 
        for (;;) {
                inputVideo >> input_frame;
@@ -218,9 +216,5 @@ int main(int argc, char *argv[])
 out_delete_trail:
        delete trail;
 out:
 out_delete_trail:
        delete trail;
 out:
-       delete drawing_method;
-       delete segmentation_method;
-       delete output_file;
-       delete input_file;
        return ret;
 }
        return ret;
 }