0

I have a route class. In header, i defined

private:
QVector<QPoint> cameraPoints;

In class source,

void Route::SetCameraPositions(QVector<QPoint> *cam)
{
   QVector<QPoint> bla;
   QPoint t;
   int x,y;
   for(int i=0; i<cam->size(); i++) {
      x = cam->at(i).x();
      y = cam->at(i).y();
      t.setX(x);
      t.setY(y);
      bla.push_back(t) //Works
      cameraPoints.push_back(t); //Doesn't work
   }
}

I can't push_back vector defined in header, but i can use push_back on vector which defined in same function. I also tried std::vector but got same error.

Here is valgrind report;

==3024==
==3024== Process terminating with default action of signal 11 (SIGSEGV)
==3024== General Protection Fault
==3024== at 0x410CA5: QBasicAtomicInt::operator!=(int) const (qbasicatomic.h:75)
==3024== by 0x417AEA: QVector<QPoint>::append(QPoint const&) (qvector.h:575)
==3024== by 0x4171C2: QVector<QPoint>::push_back(QPoint const&) (qvector.h:281)
==3024== by 0x420DF0: Route::SetCameraPositions(QVector<QPoint>*) (cRoute.cpp:46)
==3024== by 0x4107DA: MainWindow::SetCameraLocations() (mainwindow.cpp:678)
==3024== by 0x40C577: MainWindow::SetupModel() (mainwindow.cpp:141)
==3024== by 0x40B6CB: MainWindow::MainWindow(QWidget*) (mainwindow.cpp:48)
==3024== by 0x40B3BF: main (main.cpp:18)
==3024==
==3024== Events : Ir
==3024== Collected : 172003489
==3024==
==3024== I refs: 172,003,489
** Process crashed **
Sam
  • 7,252
  • 16
  • 46
  • 65
Ulfsark
  • 902
  • 1
  • 11
  • 26
  • 3
    Are you calling the `SetCameraPositions()` on a valid `Route` object? Inability to access data members usually implies an invalid `this` pointer. Can you show an [SSCCE](http://sscce.org/)? – Angew is no longer proud of SO Apr 17 '14 at 07:57
  • 1
    Yes, @Angew has a valid point. The problem is not in the code you have shown, but somewhere else, so we would need to see more. – László Papp Apr 17 '14 at 08:02
  • I defined Route object as pointer in mainwindow. I changed it and worked now. Thanks :) – Ulfsark Apr 17 '14 at 08:10

1 Answers1

1

Turning my comment into an answer.

This looks like you're not calling SetCameraPositions() on a valid Route object. Inability to access data members usually implies an invalid this pointer.

In C++, it is preferable to create objects with automatic storage duration ("on the stack"), as this prevents such issues. Simply like this:

Route r;
r.SetCameraPoints(whatever);

If you need dynamic lifetime for the object, you must create it dynamically, which implies you must allocate it first. In modern C++, you will then use a smart pointer to manage the object's lifetime:

std::unique_ptr<Route> r(new Route());
r->SetCameraPoints(whatever);
// Once r goes out of scope, the Route object will be deallocated.

It is not recommended to manage dynamic object lifetime manually (using raw pointers). Out of a sense of completeness, here's how you would do it, though:

Route *r = new Route();
r->SetCameraPoints(whatever);

// once you want to deallocate the object:
delete r;
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455