[odb-users] Segfault after adding relations

kontakt at msnoch.pl kontakt at msnoch.pl
Wed Sep 28 12:46:21 EDT 2011


 Hi

 Code is like this:

 line.h:

 #ifndef LINE_H
 #define LINE_H

 #include <QString>
 #include <QVector>

 #include <odb/core.hxx>
 #include <boost/shared_ptr.hpp>

 #include "stop.h"
 class Stop;

 #pragma db object
 class Line
 {
 public:
     Line();

     friend class odb::access;
 
     QString name() const;
     void setName(const QString& p_name);
     int variation() const;
     void setVariation(int p_variation);
     QString description() const;
     void setDescription(const QString& p_description);
     unsigned int id() const;
     void setId(unsigned int p_id);
     void addStop(boost::shared_ptr<Stop> stop);
     QVector<boost::shared_ptr<Stop> > stops();

 private:


     #pragma db id auto
     unsigned int m_id;

     #pragma db not_null
     QString m_name;
     #pragma db not_null
     QString m_description;
     #pragma db not_null
     int m_variation;

     QVector<boost::shared_ptr<Stop> > m_stops;
 };


 #endif // LINE_H

 stop.h:

 #ifndef STOP_H
 #define STOP_H

 #include <QString>
 #include <odb/core.hxx>
 #include <boost/shared_ptr.hpp>
 #include <boost/weak_ptr.hpp>

 #include "arrival.h"
 #include "line.h"

 class Line;
 class Arrival;

 #pragma db object
 class Stop
 {
 public:
     friend class odb::access;

     Stop();
     void setId(unsigned int id);
     unsigned int getId() const;
     void setName(QString name);
     QString getName() const;
     void setStreet(QString street);
     QString getStreet() const;

     void addLine(boost::shared_ptr<Line> p_linePtr);
     void addArrival(boost::shared_ptr<Arrival> p_arrivalPtr);

     bool operator ==(const Stop& rhs) const;
     QVector<boost::weak_ptr<Line> > lines();
     QVector<boost::weak_ptr<Arrival> > arrivals();
     QString test() { return "Works fine"; }

 private:
     #pragma db id auto
     unsigned int m_id;
     #pragma db not_null
     QString m_name;
     #pragma db not_null
     QString m_street;
     #pragma db not_null
     unsigned int m_ZdikId;

     #pragma db inverse(m_stops)
     QVector<boost::weak_ptr<Line> > m_lines;
     #pragma db inverse(m_stop)
     QVector<boost::weak_ptr<Arrival> > m_arrivals;
 };

 #endif // STOP_H



 selection:

    using namespace odb::core;

    typedef odb::query<Stop> query;
    typedef odb::result<Stop> result;

     try
     {
         shared_ptr<database> db(new odb::pgsql::database("postgres", 
 "postgres", "cnav", "localhost", 5432));
         transaction t(db->begin());

         result start(db->query<Stop>(query::name == p_from || 
 query::street == p_from));

         for(result::iterator i(start.begin()); i!= start.end(); ++i)
         {

             cout << "Found " << i->test().toStdString() << endl;

         }

         t.commit();
         cout << "Route Done (" << start.size() << ")" << endl;
     }
     catch(const odb::exception& e)
     {
         cerr << "Cannot execute query cause of " << e.what() << endl;
         return;
     }

 On Wed, 28 Sep 2011 13:45:15 +0200, Boris Kolpackov wrote:
> Hi Michal,
>
> kontakt at msnoch.pl <kontakt at msnoch.pl> writes:
>
>> I'm wondering why i have segmentation fault in code like this:
>>
>> transaction t(db->begin());
>> result start(db->query<Stop>(query::name == p_from || query::street 
>> ==
>> p_from));
>>
>> for(result::iterator i(start.begin()); i!= start.end(); ++i)
>> {
>>   cout << "Found " << i->getStreet().toStdString() << endl;
>> }
>>
>> t.commit();
>>
>> everything was fine until I started using relations. Stop have 
>> shared_ptr
>> to other class, and other class have weak_ptr to Stop.
>
> It's hard to say without looking at the code and I don't see anything
> suspicious in what you have shown. Would you be able to create a 
> small
> but complete test case that reproduces this problem?
>
> Boris



More information about the odb-users mailing list