Results 1 to 9 of 9

Thread: Segmentation fault

  1. #1
    Join Date
    Nov 2012
    Beans
    13

    Segmentation fault

    Code in my DataBase.cpp file:

    Code:
    #include "DataBase.h"
     
    #include <sqlite3.h>
    #include <string.h>
    #include <wx/msgdlg.h>
     
     
    bool CanClose(void)
    {
        sqlite3 *Sqlite;
        sqlite3_stmt *sqlstmt;
        char *result;
        if(sqlite3_open("SysConfig",&Sqlite)==SQLITE_OK)
        {
            sqlite3_prepare(Sqlite,"SELECT config_value FROM configuration WHERE config_id = 1;",-1,&sqlstmt,NULL);
            sqlite3_step(sqlstmt);
            result = (char*)sqlite3_column_text(sqlstmt,0);
            sqlite3_close(Sqlite);
            if(strcmp(result,"YES")==1)    //Error Here
                return true;
            else
                return false;
        }
        else
        {
            wxMessageBox(_("Cannot Find System File!"),_("Error!"));
            sqlite3_close(Sqlite);
            return false;
        }
    }
    My program was behaving abruptly..
    When I started debuging the line indicated above (line 19) is giving some error as

    program recieved signal SIGSEGV, Segmentation fault.

    further dissasembly of the statement show error at a assembly instruction

    call 0x80500b0 <strcmp@plt>

    Please Help.
    I am using CodeBlocks with GCC

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Segmentation fault

    I don't know the sqlite api very well but my guess is that either the result string is deleted by sqlite3_close(Sqlite) before you use it or it is NULL in the first place

    also don't check the return of strcmp against 1, it is not defined to do that and also probably won't in all cases you expect it too.
    only compare against equal zero or greater/smaller

  3. #3
    Join Date
    Nov 2012
    Beans
    13

    Re: Segmentation fault

    Quote Originally Posted by MadCow108 View Post
    I don't know the sqlite api very well but my guess is that either the result string is deleted by sqlite3_close(Sqlite) before you use it or it is NULL in the first place

    also don't check the return of strcmp against 1, it is not defined to do that and also probably won't in all cases you expect it too.
    only compare against equal zero or greater/smaller
    I see....

    Well using strcpy would Help?

  4. #4
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: Segmentation fault

    Quote Originally Posted by MTariq View Post
    I see....

    Well using strcpy would Help?
    Too complicated (and will introduce a memory leak, as you would have to free the the string again before returning, and there goes your old problem). Something like this would be more appropriate:

    Code:
    result = (char*)sqlite3_column_text(sqlstmt,0);
    bool result = (strcmp(result,"YES")==1);
    sqlite3_close(Sqlite);
    return result;
    In other words: First compute your result, then clean up, and finally return the result, and everything is in order.
    Last edited by Zugzwang; December 2nd, 2012 at 10:36 PM.

  5. #5
    Join Date
    Nov 2012
    Beans
    13

    Re: Segmentation fault

    Not working even..
    Seems that
    result = (char*)sqlite3_column_text(sqlstmt,0);

    is returning nothing. And result is empty..
    I found this bug by printing it on the message box...
    But why is it so.. I mean why
    result = (char*)sqlite3_column_text(sqlstmt,0);
    is returning an empty memory space....

  6. #6
    Join Date
    Jul 2008
    Location
    England
    Beans
    866

    Re: Segmentation fault

    After having a read of the documentation it would be interesting to know what "sqlite3_step" returned. The information here (http://www.sqlite.org/c3ref/step.html) indicates that there are a few different options. Maybe you are failing and don't know about it.

    Do you know if the values in the column you are looking to return are strings? Are you definitely returning the correct datatype? Here is some more documentation: http://www.sqlite.org/c3ref/column_blob.html

    In addition, the first link implies that there are newer versions of the "prepare" function and the one that you are using could be a legacy version. It would be worth updating to use the new interface.

    Paul
    My current project: http://apps.facebook.com/beatthetexan - Creating an artificial poker player using neural networks and genetic algorithms.
    My blog: http://pm-gaming.blogspot.com

  7. #7
    Join Date
    Nov 2012
    Beans
    13

    Re: Segmentation fault

    Putting Some More checks revealed that sqlite3_prepare() is returning a SQLITE_ERROR where
    #define SQLITE_ERROR 1 /* SQL error or missing database */

  8. #8
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Segmentation fault

    Well there you go then. Nothing to do with your code.

    well, not your C. Is that sql statement definitely correct?
    Last edited by muteXe; December 3rd, 2012 at 05:36 PM.

  9. #9
    Join Date
    Nov 2012
    Beans
    13

    Re: Segmentation fault

    I copied the statement from my c Code, pasted it in SQLiteman. Statement is working well and result is as desired!

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
  •