3

i am currently writing my first C++ program and so far, even if it's not complete, i wanted to try some test runs, after dealing with all compiler errors. I'll provide most of the code in the following links: http://pastie.org/8196032 http://pastie.org/8196025 Since i'm just allowed to post 2 links the header for ImageComparison is missing, which is not very important i think. stdafx.h includes required opencv and std libs. I ran gdb and got this result:

Program received signal SIGSEGV, Segmentation fault.
0x000000000040655c in cv::Mat::release() ()
(gdb) bt
>#0  0x000000000040655c in cv::Mat::release() ()
>#1  0x0000000000406410 in cv::Mat::operator=(cv::Mat const&) ()
>#2  0x00000000004052ed in ImageComparison::LoadImages() ()
>#3  0x000000000040518e in ImageComparison::DoImCo() ()
#4  0x0000000000405019 in main () 

Which doesn't give me any clue what's wrong here. I apologize if my question is just to dump, but i appreciate any suggestions.

3
  • 2
    you should post the shortest piece of code that can be used to reproduce your problem, not all of it. Commented Aug 1, 2013 at 7:49
  • sure but i thought my code is very straightforward, easy to understand, and in most cases people ask to provide more code Commented Aug 1, 2013 at 7:52
  • probably won't be needed here: at first glance of the LoadImages function I see char* buffer; ItoS( i, buffer ); and also images[i-1] when images has no elements. Any of these is 99.9% likely to be the culprit. This ic c++, use std::string. Commented Aug 1, 2013 at 7:56

2 Answers 2

3

In the lines:

vector<Mat> images;

and

images[i-1] = imread("ImageData/"+m_object_type+"/"+m_object_type+"-"+buffer+".jpeg", 1);

You have declared vector images but its size is still unspecified. Therefore you can not assign its elements according to indices. Instead, you can push_back new Mat elements:

images.push_back(imread("ImageData/"+m_object_type+"/"+m_object_type+"-"+buffer+".jpeg", 1));

Or, you can initialize vector images with a size of m_number_of_images :

vector<Mat> images(m_number_of_images);
Sign up to request clarification or add additional context in comments.

1 Comment

YEAH, BOTH ANSWERS WERE IMPORTANT AND HELPED BUT I COULD ONLY PICK ONE. THANKS A LOT U 2 :)
2
char* buffer;

This line in LoadImages needs to be an actual array or at least something aquired with new.

For example:

char buffer[100];

4 Comments

but this should be valid for pointer declaration, or am i missing something? and even if i determine it's size during initialization doesn't kill the segmentation fault
Yes, it's a valid pointer. But you cannot write to a pointer, that just points to a random address in memory. That will segfault (if you are lucky). You need to own the memory that the pointer points to. Refer to the other answer to check the other mistakes.
YEAH, BOTH ANSWERS WERE IMPORTANT AND HELPED BUT I COULD ONLY PICK ONE. THANKS A LOT U 2 :)
@Auberotte feel free to upvote the answers that were helpful in addition to picking an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.