Like Tree1Likes

Segmentation fault

This is a discussion on Segmentation fault within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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
    407
    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.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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
    2,421
    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
    2,578
    I suggest reading about the return value meanings.

    strcmp - C++ Reference

    Also, suggest checking for NULL before doing strcmp.

    Tim S.
    Salem likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    21,743
    By the way, why are you not using sqlite3_prepare_v2?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,743
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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#......

Page 1 of 2 12 LastLast
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, 02:28 PM
  2. Segmentation fault.
    By Dizzy++ in forum C Programming
    Replies: 12
    Last Post: 12-12-2010, 09:57 PM
  3. Segmentation fault
    By bijan311 in forum C++ Programming
    Replies: 3
    Last Post: 12-12-2010, 10: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, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21