c++ - Segmentation Fault in push_back() -


i getting segmentation fault in push_back(), have given below sample code of project. not using image(iplimage*) inside vec since clearing temp (iplimage*) after push_back()

my doubt this, should replace...

a.cands.push_back(b); 

...with...

 b.frontimg = null;  a.cands.push_back(b); 

...?

the program:

#include<iostream> #include<vector> #include<highgui.h> #include<cv.h>  using namespace std;  struct b {   iplimage* frontimg;   vector<int> num;   b()   {     frontimg = null;   } };   struct {   vector<b> cands;   iplimage* img1;   a()   {     img1 = null;   } };  vector<a> vec;  int main() {   (int = 0; < 1000; i++)   {     struct b b;     iplimage* temp = cvloadimage("logo.jpg");     b.frontimg = temp;      struct a;      (int j = 0; j<1000; j++)     {       a.cands.push_back(b);     }      vec.push_back(a); //here      cvreleaseimage(&temp);      //some porcess   } } 

error message comments

 #0 0x000000353a0328a5 in raise () /lib64/libc.so.6    #1 0x000000353a034085 in abort () /lib64/libc.so.6    #2 0x000000353a0707b7 in __libc_message () /lib64/libc.so.6    #3 0x000000353a0760e6 in malloc_printerr () /lib64/libc.so.6    #4 0x000000353a079b64 in _int_malloc () /lib64/libc.so.6    #5 0x000000353a07a911 in malloc () /lib64/libc.so.6    #6 0x0000000000688a7d in operator new(unsigned long)    #7 ???    #8 0x0000000000563fdd in std::_vector_base<cvpoint, std::allocator<cvpoint> >::_m_allocate (this=0x7f31ad14bb90, __n= 1096) @ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_ve‌​ctor.h:140    #9 0x0000000000560491 in std::_vector_base<cvpoint, std::allocator<cvpoint> >::_vector_base (this=0x7f31ad14bb90, __n= 1096, __a=...) @ /usr/lib/gcux/4.4.7./include/c++/4.4.7/bits/stl_vector.h:113    #10 0055ebc1 in std::vector<cvpoint, std::allocator<cvpoint> >::vector (this=0x7f31ad14bb90, __x= std::vector of length 1096, capacity 1096 = {../include/c++/    #11 ???    #12 ???    #13 ???    #14 0x000000000055f346 in std::vector<lpcandidate, std::allocator<lpcandidate> >::push_back (this=0x7f31ac00ddb0, __x= ...) @ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_ve‌​ctor.h:737    #15 0x0000000000556689 in klpr::trmanager::segregatelane (this=0x7f31ac0008e8, candlpv= std::vector of length 6, capacity 32 = {...}, candindex=0x7f31b3ffd500) @ myproject/src/trmanager.cpp:2026   

the code posted looks dangerous

  • raw pointers
  • no destructor / copy constructor / assignment operator (rule of 3 (c++03), rule of 5 (c++11)
  • no clear ownership of data.
  • potential use of data after delete.
  • missing check if clloadimage fails.

see comments:

struct b b; iplimage* temp = cvloadimage("logo.jpg"); b.frontimg = temp;  struct a;  (int j = 0; j<1000; j++)   a.cands.push_back(b); 

now have 1000 elements pointing image returned cvloadimage, assume load different images each b.frontimg in real program.

vec.push_back(a); //here 

a's default copy constructor called , if a not used anymore as default destructor call, deletes bs in vector. none of faulty in shown program part.

cvreleaseimage(&temp); 

now release 1000 elements pointing (if cvloadimage, again not problem if b.frontimg not used later.

//some process 

here don't use released image through pointer in b.


now question, yes should use

b.frontimg = null; a.cands.push_back(b); 

as removes lot of problems no 1 can copy or delete image through b (if remember delete image after).


an example of how raw pointers dangerous, add

~b() { delete frontimg; } // or user of b decides delete frontimg itself. 

to code, no copy constructor or copy assignment operator. every time

a.cands.push_back(b);  // implicit copy construct of new b. 

b later destroyed when runs out of scope. push b point deleted image, , when delete a delete image again through vector's b, corrupting heap.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -