-1

I have passed an image from one function/method to another several times before. I have seen this question at SO also but i don't know why i am getting a segmentation fault when i am trying to pass an image from main() to another function in the same file. I have already tried to display the image to make sure that the image is getting loaded correctly.

using namespace cv;

void checkFunction(Mat image)
{
    imshow("myimage", image);
}

void main()
{

    Mat img = imread("myImage.png", 1);
    imshow("display image", img);
    checkFunction(img);
    waitKey(0);

}
Community
  • 1
  • 1
skm
  • 5,015
  • 8
  • 43
  • 104
  • reconsider your spelling. – berak Nov 17 '14 at 17:33
  • 2
    Pass by reference, not by value. Passing by value incurs a copy. – PaulMcKenzie Nov 17 '14 at 17:35
  • `imshow("display image", img);` and `imread("myImage.png", 1);` I will check what is happening here if I were you. What does imread return ? – DumbCoder Nov 17 '14 at 17:37
  • @PaulMcKenzie, you're wrong. assume cv::Mat to be some sort of smart-pointer – berak Nov 17 '14 at 17:42
  • @berak: sorry i did not understand, which spelling are you talking about? – skm Nov 17 '14 at 17:43
  • @berak - There is still copying going on, regardless of what's underneath the hood. Passing by (const) reference guarantees that *no* copy will occur. – PaulMcKenzie Nov 17 '14 at 17:50
  • @PaulMcKenzie unfortunately it does not secure that, call it a design error. it does though state the programmer's intention. – berak Nov 17 '14 at 17:56
  • 1
    @skm, if you let 'Fucntion' go to production code, you're a sloppy person in general. – berak Nov 17 '14 at 17:58
  • @berak: oh i noticed it but actually i wrote it just as an example at SO. Its not the original part of my code...but yeah, it was my mistake – skm Nov 17 '14 at 18:01

1 Answers1

0

For large objects, you should declare them outside the function, or use dynamic memory.

In most compilers, the size of the stack (where local variables are stored), is much smaller than the area for global variables and dynamic memory.

Also, prefer passing large objects by constant reference (if it won't be changed) or reference (if it will be modified). Otherwise use smart pointers.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 1
    `checkFucntion(const Mat& image)` has solved my problem but i really want to know why am i getting segmentation fault with the previous method. I have used that method before and it worked fine then, why am i getting segmentation fault today? – skm Nov 17 '14 at 17:42
  • 1
    Supposing that `Mat` is too large a type, pushing a `Mat` object onto the stack (which is what happens when you pass-by-copy) will cause a... [stack overflow](http://en.wikipedia.org/wiki/Stack_overflow) :D and since this amounts to writing to memory addresses that the OS has not dedicated to your program's stack, the OS notifies you that, hey, you disrespected the memory segmentation it had put in place, hence the name. – jrsala Nov 17 '14 at 17:46
  • The code which i have mentioned above is a part of a bigger project. That bigger project with the same file is working then, why can't my smaller version work with that image....sorry, i am really unable to understand it – skm Nov 17 '14 at 17:57
  • @skm - If you changed your function to take a reference, does the issue go away? If it does, then it is the copy operation going on inside of `Mat` that is causing the issue, and copying a `Mat` is not that straightforward (acts like a smart pointers). – PaulMcKenzie Nov 17 '14 at 18:08
  • @PaulMcKenzie: yeah it goes away by using "&" but now i am getting segmentation fault when i am trying to do `this->newImage = image`. I don't know why i am getting fault in my smaller version of code while the same kind of code is working in the bigger project. – skm Nov 17 '14 at 18:11
  • @skm - With the line `this->newImage = image`, you're invoking the assignment operator, which does the same thing as the copy constructor. Since `Mat` is implemented similar to a smart pointer, you need to see exactly where the error occurs during the copy. – PaulMcKenzie Nov 17 '14 at 18:22
  • @PaulMcKenzie: sorry, i don't know how can i check it – skm Nov 17 '14 at 18:47
  • @PaulMcKenzie: I tried to use "this->image = image.clone()" but still i am getting segmentation fault. – skm Nov 17 '14 at 19:03
  • 1
    Many openCV methods are allowed to reallocate the data pointer, which wont be "taken back" if called the mat by value. – Micka Nov 17 '14 at 19:41