Thread: Segmentation fault

  1. #1
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118

    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.

  2. #2
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    I am using CodeBlocks with GCC in linux

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Well since I haven't used much sqlite I don't see where your mistake is, but since the error means you are accessing memory you shouldn't be then result probably isn't what you think it is. Have you tried just displaying this variable to the screen to see what its value is?

    Sorry if I am way off base but it seems like you set the pointer to point to a memory position then you close that memory position right after. If the compiler isn't allowed to acces that memory after the close function then the strcmp() probably creates a seg fault. Maybe set an int to recieve the return of strcmp then close the file then do a return based on the int.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Three problems:
    First, you are using SQLite - a C library. There are C++ wrappers. I strongly recommend you get one.
    Second, result may be NULL. Obviously comparing a NULL pointer will get you nowhere except into undefined land (probably crash).
    Third, who owns the memory returned by sqlite3_column_text? You? Or Sqlite? What happens when you call close? Does it free the memory? Or do you have to?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Quote Originally Posted by Elysia View Post
    Three problems:
    First, you are using SQLite - a C library. There are C++ wrappers. I strongly recommend you get one.
    Second, result may be NULL. Obviously comparing a NULL pointer will get you nowhere except into undefined land (probably crash).
    Third, who owns the memory returned by sqlite3_column_text? You? Or Sqlite? What happens when you call close? Does it free the memory? Or do you have to?
    1. I will surely look for it although I thought that a c library should work well with c++. But still as you have told me. A c++ library with classes in it would be much better to use. Thanks for this.

    2. You are right. The result

    result = (char*)sqlite3_column_text(sqlstmt,0);

    is throwing out is NULL. (I must have look for it before).

    3. I Changed the code and closed the DB after I have check for the string but it didn't worked...(I did it before step 2)
    Code:
    bool res=(strcmp(result,"YES")==1);
            sqlite3_close(Sqlite);
            return res;
    I will surely look for the SQLite C++ version. But Still Can you tell me why the above mentioned statement is returning a NULL. Though all the code is correctly carved.

  6. #6
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Putting Some More checks revealed that sqlite3_prepare() is returning a SQLITE_ERROR where
    #define SQLITE_ERROR 1 /* SQL error or missing database */

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by C_programmer.C View Post
    Code:
    if(strcmp(result,"YES")==1)    //Error Here
    strcmp() returns 0 when the strings are equal. 1 is returned when the compared string is "greater." look at the docs to know what that means. I'm not sure if that's what you intended

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest reading about the return value meanings.

    strcmp - C++ Reference

    Also, suggest checking for NULL before doing strcmp.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Quote Originally Posted by stahta01 View Post
    I suggest reading about the return value meanings.

    strcmp - C++ Reference

    Also, suggest checking for NULL before doing strcmp.

    Tim S.
    Quote Originally Posted by Elkvis View Post
    strcmp() returns 0 when the strings are equal. 1 is returned when the compared string is "greater." look at the docs to know what that means. I'm not sure if that's what you intended
    please read my previous post. The talk about return value of strcmp comes later. First I have to resolve the problem that is occurring in sqlite3_prepare() statement.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, why are you not using sqlite3_prepare_v2?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    I have used it! Same result....

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's just confirm this: when you only call sqlite3_prepare_v2, you get the error? I note that your attempt to use sqlite3_column_text is wrong: once you use sqlite3_close, the memory location is invalidated. You should copy the result first, e.g., into a std::string, perhaps calling sqlite3_column_bytes() to find out the size of the result first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    I think it would be good if I post the new DataBase.cpp
    Code:
    #include "DataBase.h"
    
    #include <sqlite3.h>
    #include <string.h>
    #include <wx/msgdlg.h>
    
    
    bool CanClose(void)
    {
        sqlite3 *Sqlite;
        sqlite3_stmt *sqlstmt;
        char *result;
        const char*tal;
        bool res;
        if(sqlite3_open("SysConfig",&Sqlite)==SQLITE_OK)
        {
            int i=sqlite3_prepare_v2(Sqlite,"SELECT * FROM configuration WHERE config_id = 1;",100,&sqlstmt,&tal);
            if(i==SQLITE_OK)
            if(sqlite3_step(sqlstmt)==SQLITE_ROW)
            {
                result = (char*)sqlite3_column_text(sqlstmt,0);
                wxString str;
                wxMessageBox(str.FromAscii(result));
                res=(strcmp(result,"YES")==1);
                sqlite3_close(Sqlite);
                return res;
            }
            else wxMessageBox(_("Statement Execution Error!"),_("Error!"));
            else wxMessageBox(_("Cannot Prepare Statement"));
    
            return 0;
        }
        else
        {
            wxMessageBox(_("Cannot Find System File!"),_("Error!"));
            sqlite3_close(Sqlite);
            return false;
        }
    }
    now here I am getting that message box with "Cannot Prepare Statement"....
    I copied the sql statement and pasted in sqliteman which gave me the desired result with out any error.
    further more sqlite3_prepare_v2 is returning SQLITE_ERROR...

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you can print actual error codes instead of the vague and uninformative "it doesn't work"
    Result Codes

    > res=(strcmp(result,"YES")==1);
    As already mentioned, strcmp() does NOT return 1 when the strings match.
    In fact, it doesn't return 1 at all (except for some special circumstances).

    If you have
    res = strcmp(result,"YES");
    then you have
    if ( res == 0 )
    if ( res < 0 )
    if ( res > 0 )
    There is no == 1 test!

    Read the manual page for everything you're using. If you're this badly wrong on something simple like strcmp(), then there's no hope for you when it comes to SQLite.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Well! the talk of what strcmp returns would come far after I get some success in properly executing sqlite3_prepare_v2 then sqlite3_step and then strcmp. And then would be the time I should google for the return values of strcmp or some other basic functions.
    And the reason I have made this mistake is that I am returning to C/C++ after a time span of about 10 months. I was busy with development in C#, and you might know that there is no concept of strcmp and its return values in C#......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault?
    By ronaldh12 in forum C Programming
    Replies: 4
    Last Post: 02-11-2012, 03:28 PM
  2. Segmentation fault.
    By Dizzy++ in forum C Programming
    Replies: 12
    Last Post: 12-12-2010, 10:57 PM
  3. Segmentation fault
    By bijan311 in forum C++ Programming
    Replies: 3
    Last Post: 12-12-2010, 11:45 AM
  4. segmentation fault
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 09-29-2005, 02:13 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM