Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Segmentation fault

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Segmentation fault

    Hello again!

    I have following code:

    Qt Code:
    1. CBrowserWindow::CBrowserWindow(QWidget *parent, MySQLConnect* pDataBase, QString sTableName)
    2. : QWidget(parent)
    3. {
    4. Q_CHECK_PTR(pDataBase); // if database connection is NULL, we have a big problem
    5.  
    6. m_pModel=new QSqlRelationalTableModel(this); // creates new model
    7. Q_CHECK_PTR(m_pModel); // checks for succesful creation
    8. m_pModel->setTable(sTableName); // sets table name
    9.  
    10. m_pColumnNames=new QStringList(); // creates new string list
    11. Q_CHECK_PTR(m_pColumnNames); // checks for succesful creation
    12.  
    13. // fetches record
    14. m_pTableRecord=new QSqlRecord(); // creates new record
    15. Q_CHECK_PTR(m_pTableRecord); // checks for susccesful creation
    16. //m_pTableRecord=&pDataBase->m_pDatabase->record(sTableName); // fetches column names
    17. m_pTableRecord=&pDataBase->m_pDatabase->record(sTableName); // fetches column names
    18.  
    19.  
    20. if (!m_pTableRecord->isEmpty()) {
    21. for (m_iIndex=0; m_iIndex<m_pTableRecord->count(); m_iIndex++) {
    22. m_pColumnNames->append(m_pTableRecord->fieldName(m_iIndex));
    23. }
    24. } // if
    25.  
    26. createBrowserContens(pDataBase, m_pColumnNames); // creates browser controls
    27.  
    28. delete m_pModel; // deletes pModel;
    29. delete m_pTableRecord; // deletes m_pTableRecord
    30. // just to be sure
    31. m_pModel=0;
    32. m_pTableRecord=0;
    33. }
    To copy to clipboard, switch view to plain text mode 
    and in the line
    Qt Code:
    1. m_pTableRecord=&pDataBase->m_pDatabase->record(sTableName); // fetches column names
    To copy to clipboard, switch view to plain text mode 
    I get segmentation fault. What I am trying to do is to dynamically fetch table column names from mysql database and then create table widget for record display. Why do I get segfault?

    Here is class description:
    Qt Code:
    1. class CBrowserWindow : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. CBrowserWindow(QWidget *parent = 0, MySQLConnect* pDataBase=0, QString sTableName="");
    6.  
    7. ~CBrowserWindow();
    8.  
    9. private:
    10. QStringList* m_pColumnNames; // column names for selected database
    11.  
    12. private:
    13. // buttons
    14. QPushButton* m_pButtonAdd; // button for addition of record
    15. QPushButton* m_pButtonChange; // button for change of record
    16. QPushButton* m_pButtonDelete; // button for deletion of record
    17. QPushButton* m_pButtonSelect; // button for selection of record
    18.  
    19. private:
    20. // layouts
    21. QHBoxLayout* m_pHorizLayout; // horizontal leyout
    22. QVBoxLayout* m_pVertLayout; // vertical layout
    23.  
    24. private:
    25. // mebmers needed for column idetification
    26. QSqlRelationalTableModel* m_pModel; // pointer to pmodel
    27. QSqlRecord* m_pTableRecord; // pointer to database record
    28. QTableView* m_pView; // pointer to view
    29. QGridLayout* m_pGridLayout; // pointer to grid layout
    30. quint16 m_iIndex; // index in for loops - this type is guaranteed to be 16-bit on all platforms supported by Qt
    31.  
    32. private:
    33. // private methods
    34. void createBrowserContens(MySQLConnect* pDatabase, QStringList* pColumnNames); // creates table on widget
    35. };
    36.  
    37. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    I think because you're taking the address of a temporary QSqlRecord -- the one returned by record(...).
    Don't use a pointer to a QSqlRecord or do it like this:
    Qt Code:
    1. QSqlRecord rec=pDataBase->m_pDatabase->record(sTableName); // fetches column names
    2. m_pTableRecord = new QSqlRecord( rec );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    I've changed the code in the way you instructed, i get same segmentation fault.
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    Then you must have a NULL pointer somewhere. What about m_pDatabase? Are you sure it is always valid?

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    Well, I've checked with gdb, none of actors pointer have NULL or 0 value, according to gdb. What the heck is going on??
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    Could you post the backtrace?

  7. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    Quote Originally Posted by jacek View Post
    Could you post the backtrace?
    I am using gdb through KDevelop, how do I get backtrace?
    Qt 5.3 Opensource & Creator 3.1.2

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    Quote Originally Posted by MarkoSan View Post
    I am using gdb through KDevelop, how do I get backtrace?
    I've never tried it, but there's a "GDB" tab where you can issue commands. In standalone GDB you do it this way:
    $ gdb ./app
    (gdb) run
    <program crashes>
    (gdb) bt
    <backtrace>

  9. #9
    Join Date
    Oct 2007
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation fault

    Hi,

    Quote Originally Posted by MarkoSan View Post
    I am using gdb through KDevelop, how do I get backtrace?
    With Kdevelop, you have got a tab called "backtrace" when you run your application in debug mode.

    Bye.
    anonyme_84

  10. #10
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    Well, my friends, here is a backtrace:
    Qt Code:
    1. (gdb) run
    2. Starting program: /home/markofr/working_copy/qKobilica/bin/qkobilica
    3. [Thread debugging using libthread_db enabled]
    4. [New Thread 47848586376464 (LWP 9931)]
    5. Qt: gdb: -nograb added to command-line options.
    6. Use the -dograb option to enforce grabbing.
    7.  
    8. Program received signal SIGSEGV, Segmentation fault.
    9. [Switching to Thread 47848586376464 (LWP 9931)]
    10. 0x0000000000000000 in ?? ()
    11. (gdb) bt
    12. #0 0x0000000000000000 in ?? ()
    13. #1 0x00002b8496d9e4e3 in QSqlDatabase::record () from /usr/lib/libQtSql.so.4
    14. #2 0x00000000004072c8 in CBrowserWindow (this=0x6cff50, parent=0x663e70, pDataBase=0x66f220, sTableName=@0x7fff13f42830) at cbrowserwindow.cpp:17
    15. #3 0x0000000000405b48 in qKobilica::browseSifrantKrajev (this=0x663e70) at qkobilica.cpp:91
    16. #4 0x0000000000408a5d in qKobilica::qt_metacall (this=0x663e70, _c=QMetaObject::InvokeMetaMethod, _id=0, _a=0x7fff13f42e00) at moc_qkobilica.cpp:66
    17. #5 0x00002b8497c2f770 in QMetaObject::activate () from /usr/lib/libQtCore.so.4
    18. #6 0x00002b8497c2f969 in QMetaObject::activate () from /usr/lib/libQtCore.so.4
    19. #7 0x00002b849718d9ab in QAction::triggered () from /usr/lib/libQtGui.so.4
    20. #8 0x00002b849718f4b8 in QAction::activate () from /usr/lib/libQtGui.so.4
    21. #9 0x00002b849754fea9 in ?? () from /usr/lib/libQtGui.so.4
    22. #10 0x00002b849755264a in QMenu::mouseReleaseEvent () from /usr/lib/libQtGui.so.4
    23. #11 0x00002b84971e15d9 in QWidget::event () from /usr/lib/libQtGui.so.4
    24. #12 0x00002b849754de85 in QMenu::event () from /usr/lib/libQtGui.so.4
    25. #13 0x00002b8497194d51 in QApplicationPrivate::notify_helper () from /usr/lib/libQtGui.so.4
    26. #14 0x00002b8497195949 in QApplication::notify () from /usr/lib/libQtGui.so.4
    27. #15 0x00002b8497c1e846 in QCoreApplication::notifyInternal () from /usr/lib/libQtCore.so.4
    28. #16 0x00002b849719fe73 in ?? () from /usr/lib/libQtGui.so.4
    29. #17 0x00002b84971fa37c in ?? () from /usr/lib/libQtGui.so.4
    30. #18 0x00002b84971f8239 in QApplication::x11ProcessEvent () from /usr/lib/libQtGui.so.4
    31. #19 0x00002b8497226599 in ?? () from /usr/lib/libQtGui.so.4
    32. #20 0x00002b8499f8ffd3 in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0
    33. #21 0x00002b8499f932dd in ?? () from /usr/lib/libglib-2.0.so.0
    34. #22 0x00002b8499f9380e in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0
    35. #23 0x00002b8497c45f4e in QEventDispatcherGlib::processEvents () from /usr/lib/libQtCore.so.4
    36. #24 0x00002b8497225e07 in ?? () from /usr/lib/libQtGui.so.4
    37. #25 0x00002b8497c1bb31 in QEventLoop::processEvents () from /usr/lib/libQtCore.so.4
    38. #26 0x00002b8497c1bc86 in QEventLoop::exec () from /usr/lib/libQtCore.so.4
    39. #27 0x00002b8497c1effc in QCoreApplication::exec () from /usr/lib/libQtCore.so.4
    40. #28 0x00002b8497194928 in QApplication::exec () from /usr/lib/libQtGui.so.4
    41. #29 0x00000000004062fd in main (argc=1, argv=0x7fff13f44638) at main.cpp:13
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    Are you sure you instantiate the QSqlDatabase? It doesn't look like you do.
    Can you post the code where you create it? It might be the fact that QSqlDatabase::addDatabase gives you a local copy and you take its address. The pointer will become invalid by the time you pass it to the constructor.
    Why don't you use const refs instead?

  12. #12
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    Well, this is a code:
    Qt Code:
    1. // sets up database - params QString db_user,QString db_pass,QString db_name,QString host,QString type
    2. m_pDatabaseConnection=new MySQLConnect(QString("username"), QString("password"), QString("premade_database"));
    3. Q_CHECK_PTR(m_pDatabaseConnection);
    To copy to clipboard, switch view to plain text mode 

    I am quite sure I will be forced to rewrite the MySQLConection class since it obviously does not work!!!
    Qt 5.3 Opensource & Creator 3.1.2

  13. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    Yes, but I was referring to the internal QSqlDatabase pointer. The one from MySqlConnect.

  14. #14
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    Ok, just a second. Did you mean this chunk of code:
    Qt Code:
    1. MySQLConnect::MySQLConnect(QString db_user, QString db_pass, QString db_name, QString host, QString type) {
    2.  
    3. //connect to database
    4. QSqlDatabase db = QSqlDatabase::addDatabase(type);
    5. db.setHostName(host);
    6. db.setDatabaseName(db_name);
    7. db.setUserName(db_user);
    8. db.setPassword(db_pass);
    9.  
    10. //get a pointer to a database connection
    11. m_pDatabase = &QSqlDatabase::database();
    12. Q_CHECK_PTR(m_pDatabase);
    13.  
    14. //sql table model == read/write model --> navigate and modify individual sql tables
    15. m_pModel = new QSqlRelationalTableModel(this);
    16. Q_CHECK_PTR(m_pModel);
    17.  
    18. //model->setTable(db_table);
    19. //all changes will be cached in the model until either submitAll() or revertAll() is called
    20. m_pModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    21.  
    22. //populates the model with data from the table, using the specified filter and sort conditions
    23. //model->select();
    24. }
    25.  
    26. //QSqlDatabase* MySQLConnect::m_pDatabase=0;
    27.  
    28. /*
    29. //this function is called if a refresh button is clicked
    30. void MySQLConnect::refresh_everything() {
    31.   model->select();
    32. }
    33.  
    34. void MySQLConnect::change_mysql() {
    35.   //submits all pending changes to the mysql database
    36.   model->submitAll();
    37. }
    38. */
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  15. #15
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    Rewrite it like this:
    Qt Code:
    1. MySQLConnect::MySQLConnect(QString db_user, QString db_pass, QString db_name, QString host, QString type) {
    2. //connect to database
    3. m_Database = QSqlDatabase::addDatabase(type);
    4. m_Database.setHostName(host);
    5. m_Database.setDatabaseName(db_name);
    6. m_Database.setUserName(db_user);
    7. m_Database.setPassword(db_pass);
    8. bool openedOK = m_Database.open();
    9.  
    10. m_pModel = new QSqlRelationalTableModel(this);
    11. Q_CHECK_PTR(m_pModel);
    12. m_pModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    13. ...
    14. }
    To copy to clipboard, switch view to plain text mode 

    replace m_pDatabase with m_Database (not a pointer).

  16. #16
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    Hi. is there possible fault in the message:
    Qt Code:
    1. mysqlconnect.cpp: In constructor ‘MySQLConnect::MySQLConnect(QString, QString, QString, QString, QString)’:
    2. mysqlconnect.cpp:17: warning: taking address of temporary
    To copy to clipboard, switch view to plain text mode 
    according to following code chunk:
    Qt Code:
    1. m_pDatabase = &QSqlDatabase::database();
    To copy to clipboard, switch view to plain text mode 
    ?
    Qt 5.3 Opensource & Creator 3.1.2

  17. #17
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    Come on... This is exactly what I was trying to tell you in my last posts.

  18. #18
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    Ok, Marcel sorry for stutpidity, but how do I change previous statements to make them work, can you give me an example please?
    Qt 5.3 Opensource & Creator 3.1.2

  19. #19
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    No problem. Take a look at post #15.
    You have to change m_pDatabase in class declaration. You don't need a pointer to a QSqlDatabase. You can declare it as
    Qt Code:
    1. QSqlDatabase m_Database;
    To copy to clipboard, switch view to plain text mode 
    .

    Then make the appropriate changes in the constructor, as I've shown you.
    Also, you will have to change every line of code where m_pDatabse was used, since now it is not a pointer anymore.

  20. #20
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    Ok, let me get to work now, I'll report results ...
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Strange segmentation fault
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 4th January 2009, 20:50
  2. Segmentation Fault
    By Krish_ng in forum Qt Programming
    Replies: 8
    Last Post: 7th August 2007, 11:49
  3. Process aborted. Segmentation fault
    By Pragya in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2007, 09:12
  4. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 17:35
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 17:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.