title: Segmentation fault, can't find where I did wrong
body: <p>I cannot find where I did wrong in this C++ code for my raspberry pi, it gives me a segmentation fault but after looking for 4 hours and searching here I see nothing wrong</p>
  
  <p>To my knowledge segmentation faults happen when a program is trying to access memory that isn't his, but I don't see this happening anywhere.</p>
  
  <p>I'm basically trying to find a certain coloured object with the raspberry pi module</p>
  
  <pre><code>#include &lt;cv.h&gt;
  #include &lt;highgui.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include "RaspiCamCV.h"
  
  using namespace cv;
  
  int main(int argc, char *argv[ ]){
  
      RASPIVID_CONFIG * config = (RASPIVID_CONFIG*)malloc(sizeof(RASPIVID_CONFIG));
  
      config-&gt;width=320;
      config-&gt;height=240;
      config-&gt;bitrate=0;      // zero: leave as default
      config-&gt;framerate=0;
      config-&gt;monochrome=0;
  
      int opt;
  
      while ((opt = getopt(argc, argv, "lxm")) != -1)
      {
          switch (opt)
          {
              case 'l':                                       // large
                  config-&gt;width = 640;
                  config-&gt;height = 480;
                  break;
              case 'x':                                       // extra large
                  config-&gt;width = 960;
                  config-&gt;height = 720;
                  break;
              case 'm':                                       // monochrome
                  config-&gt;monochrome = 1;
                  break;
              default:
                  fprintf(stderr, "Usage: %s [-x] [-l] [-m] \n", argv[0], opt);
                  fprintf(stderr, "-l: Large mode\n");
                  fprintf(stderr, "-x: Extra large mode\n");
                  fprintf(stderr, "-l: Monochrome mode\n");
                  exit(EXIT_FAILURE);
          }
      }
  
      /*
       Could also use hard coded defaults method: raspiCamCvCreateCameraCapture(0)
       */
      RaspiCamCvCapture * capture = (RaspiCamCvCapture *) raspiCamCvCreateCameraCapture2(0, config);
      free(config);
  
      CvFont font;
      double hScale=0.4;
      double vScale=0.4;
      int    lineWidth=1;
  
      cvInitFont(&amp;font, CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale, vScale, 0, lineWidth, 8);
  
      cvNamedWindow("RaspiCamTest", 1);
      int exit = 0;
      IplImage* x;
      IplConvKernel* erodeElement;
      IplConvKernel* dilateElement;
  
      printf("start");
  
  
      do {
          IplImage* image = raspiCamCvQueryFrame(capture);
          cvCvtColor(image, x, COLOR_BGR2HSV);
  
          cvInRangeS(&amp;x, cvScalar(0,137,171), cvScalar(48,256,248), x);
  
          char text[200];
          sprintf(text, "Press ESC to exit");
          cvPutText(x, text, cvPoint(05, 80), &amp;font, cvScalar(255, 255, 0, 0));
  
          printf("hi");
  
          erodeElement = cvCreateStructuringElementEx(3,3,-1,-1,MORPH_RECT);
          dilateElement = cvCreateStructuringElementEx(6,6,-1,-1,MORPH_RECT);
  
          cvErode(x,x,erodeElement);
          cvErode(x,x,erodeElement);
  
          cvDilate(x,x,dilateElement);
          cvDilate(x,x,dilateElement);
  
  
  
          cvShowImage("RaspiCamTest2", static_cast&lt;CvArr*&gt;(&amp;x));
          cvShowImage("RaspiCamTest", image);
  
          char key = cvWaitKey(10);
  
          switch(key)
          {
              case 27:                // Esc to exit
                  exit = 1;
                  break;
              case 60:                // &lt; (less than)
                  raspiCamCvSetCaptureProperty(capture, RPI_CAP_PROP_FPS, 25);    // Currently NOOP
                  break;
              case 62:                // &gt; (greater than)
                  raspiCamCvSetCaptureProperty(capture, RPI_CAP_PROP_FPS, 30);    // Currently NOOP
                  break;
          }
  
      } while (!exit);
  
      cvDestroyWindow("RaspiCamTest");
      raspiCamCvReleaseCapture(&amp;capture);
      return 0;
  }
  </code></pre>