IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PostgreSQL Discussion :

[postgres] [c++] segmentation fault


Sujet :

PostgreSQL

  1. #1
    Membre du Club

    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 82
    Points : 59
    Points
    59
    Par défaut [postgres] [c++] segmentation fault
    bonjour,

    j'utilise postgres comme bdd dans mon logiciel C++
    mais de temps en temps la fonction
    PQExec de la libraire postgres me fait un beau segmentation fault!
    alors que auparavant il est passer au meme endroit pas mal de fois et tout etait ok la requete marchait parfaitement...
    quelqu'un aurait une explication?

  2. #2
    Membre averti

    Homme Profil pro
    Inscrit en
    Janvier 2005
    Messages
    338
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 338
    Points : 404
    Points
    404
    Par défaut
    Le bout de code incriminé, voir une trace de debuggage serait le bien venu si tu veux que l'on t'aiguille sur un éventuel problème.
    Christophe Chauvet
    Consultant Odoo
    Python / PostgreSQL

  3. #3
    Membre du Club

    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 82
    Points : 59
    Points
    59
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    Executequery(const string& cmd) throw (Xpostgresql) : _cmd(cmd)
    {
     
         cout << "avant" << endl;
           PGresult* res;
     
     
    	try {
     
    		res = PQexec(cnx(), _cmd.c_str());
    	} catch(Xpostgresql& e){
    		cout << "-----------> ERREUR BDD" << endl;
    	}
     cout << "apres" << endl;
     
    	if (res == NULL){
    		cout << "ERREUR : res nul!" << " cmd: " << _cmd << endl;
    		throw (Xpostgresql("PQexec retourne NULL"));
    	}
     
            if (PQresultStatus(res) != PGRES_TUPLES_OK)
            {
    		string errBd(PQerrorMessage(iBd.pqCnx()->cnx()));
                    t << ESI(1) << "ERREUR : " <<  errBd << " cmd: " << _cmd << endl;
                    PQclear(res);
     
    		throw (Xpostgresql(errBd.c_str()));
    	}
    	else {
    		for (int col = 0; col < PQnfields(res); col++) {
     
    		}	
                PQclear(res);
     
    	}
    }
    voila la derniere trace que j'ai c'est avant...
    mais elle passep lein de fois dans cette fct et ca marche parfaitmeent mais d'un coup boom!!!!

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    54
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2007
    Messages : 54
    Points : 43
    Points
    43
    Par défaut
    J'ai la même erreur mais en C



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    #include <stdio.h>
    #include <stdlib.h>
    #include "libpq-fe.h"
     
    static void exit_nicely(PGconn *conn)
    {
           PQfinish(conn);
           exit(1);
    }
     
    int bande()
    {
        const char *conninfo;
        PGconn     *conn;
        PGresult   *res;
        int 	  nFields;
        int  	  i, j;
     
        char maRequete[250];
     
        int i = 1; int j = 2;
     
        conninfo = "***chaine pour se connecter***";
     
        /* Make a connection to the database */
        conn = PQconnectdb(conninfo);
     
        /* Check to see that the backend connection was successfully made */
        if (PQstatus(conn) != CONNECTION_OK)
        {
            fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
            exit_nicely(conn);
        }
     
        /* Start a transaction block */
        res = PQexec(conn, "BEGIN");
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
            PQclear(res);
            exit_nicely(conn);
        }
     
        /*
        * Should PQclear PGresult whenever it is no longer needed to avoid memory
        * leaks
        */
        PQclear(res);
     
     
        while(j<=nbPoints)
        {   strcpy(maRequete,"***une requete***");
     
            printf("%s \n",maRequete);
     
            res = PQexec(conn,maRequete);
     
            if (PQresultStatus(res) != PGRES_TUPLES_OK)
            {
                fprintf(stderr, "SELECT failed: %s \n", PQerrorMessage(conn));
                fprintf(stderr, "maRequete= %s \n", maRequete);
                PQclear(res);
                exit_nicely(conn);
            }
     
            PQclear(res);
     
            if (i==1) //Si c'est le premier segment, on crée le premier polygone
            {
                strcpy(maRequete,"***une requete***");
     
                res = PQexec(conn,maRequete);
     
                if (PQresultStatus(res) != PGRES_TUPLES_OK)
                {
                    fprintf(stderr, "SELECT failed: %s \n", PQerrorMessage(conn));
                    fprintf(stderr, "maRequete= %s \n", maRequete);
                    PQclear(res);
                    exit_nicely(conn);
                }
     
                PQclear(res);
            }else   // Sinon on fait une union avec le nouveau polygone
            {
                strcpy(maRequete,"***une requete***");
     
                printf("%s\n",maRequete);
     
                res = PQexec(conn,maRequete);
     
                if (PQresultStatus(res) != PGRES_TUPLES_OK)
                {
                    fprintf(stderr, "SELECT failed: %s \n", PQerrorMessage(conn));
                    fprintf(stderr, "maRequete= %s \n", maRequete);
                    PQclear(res);
                    exit_nicely(conn);
                }
     
                PQclear(res);
            }
     
            i++;
            j++;
        }
     
        /* end the transaction */
        res = PQexec(conn, "END");
        PQclear(res);
     
        /* close the connection to the database and cleanup */
        PQfinish(conn);
     
        return 0;
    }

  5. #5
    Membre averti

    Homme Profil pro
    Inscrit en
    Janvier 2005
    Messages
    338
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 338
    Points : 404
    Points
    404
    Par défaut
    Pourriez vous lancer vos bout de code dans GDB et lorsque le segfault survient faite simplement un bt et nous poster la trace
    Christophe Chauvet
    Consultant Odoo
    Python / PostgreSQL

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    54
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2007
    Messages : 54
    Points : 43
    Points
    43
    Par défaut
    Je me retrouve avec :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    (gdb) run
    Starting program: /dea/fsauvage/nouveau
    [Thread debugging using libthread_db enabled]
    [New Thread -1212294464 (LWP 18936)]
     
    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1212294464 (LWP 18936)]
    0xb7fca5c9 in PQconsumeInput () from /usr/lib/libpq.so.4
    (gdb) bt
    #0  0xb7fca5c9 in PQconsumeInput () from /usr/lib/libpq.so.4
    #1  0xb7fcaf07 in PQgetResult () from /usr/lib/libpq.so.4
    #2  0xb7fcb2c5 in PQputCopyEnd () from /usr/lib/libpq.so.4
    #3  0xb7fcb50f in PQexec () from /usr/lib/libpq.so.4
    #4  0x080495fe in bande () at bande.c:254
    #5  0x08049841 in main () at bande.c:315


    :-/

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    54
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2007
    Messages : 54
    Points : 43
    Points
    43
    Par défaut
    Après ce changement :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    strcpy(maRequete,"***une requete***");
     
    fflush(stdout);
     
    printf("OK1\n");
    if(PQstatus(conn) != CONNECTION_OK)
    {
    printf("PQstatus(conn) != CONNECTION_OK\n");
    }
    printf("OK2\n",maRequete);
     
    res = PQexec(conn,maRequete); /* Instruction qui génère l'erreur de segmentation */
    Le terminal m'affiche :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    fsauvage@clodomir4:~$ gcc  -I/usr/include/postgresql/ -lpq /usr/lib/libpq.a bande.c -o nouveau -lm
    bande.c: Dans la fonction «bande» :
    bande.c:19: attention : incompatible implicit declaration of built-in function «strcpy»
    bande.c:38: attention : incompatible implicit declaration of built-in function «strdup»
    bande.c:66: attention : incompatible implicit declaration of built-in function «strcat»
    bande.c:151: attention : incompatible implicit declaration of built-in function «atan»
    bande.c:154: attention : incompatible implicit declaration of built-in function «cos»
    bande.c:155: attention : incompatible implicit declaration of built-in function «sin»
    fsauvage@clodomir4:~$ nouveau
    OK1
    Erreur de segmentation
    fsauvage@clodomir4:~$ gcc -g -I/usr/include/postgresql/ -lpq /usr/lib/libpq.a bande.c -o nouveau -lm
    bande.c: Dans la fonction «bande» :
    bande.c:19: attention : incompatible implicit declaration of built-in function «strcpy»
    bande.c:38: attention : incompatible implicit declaration of built-in function «strdup»
    bande.c:66: attention : incompatible implicit declaration of built-in function «strcat»
    bande.c:151: attention : incompatible implicit declaration of built-in function «atan»
    bande.c:154: attention : incompatible implicit declaration of built-in function «cos»
    bande.c:155: attention : incompatible implicit declaration of built-in function «sin»
    fsauvage@clodomir4:~$ gdb nouveau
    GNU gdb 6.4-debian
    Copyright 2005 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i486-linux-gnu"...Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
     
    (gdb) run
    Starting program: /dea/fsauvage/nouveau
    [Thread debugging using libthread_db enabled]
    [New Thread -1212847424 (LWP 10523)]
    OK1
     
    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1212847424 (LWP 10523)]
    0xb7f41e79 in PQstatus () from /usr/lib/libpq.so.4
    (gdb) bt
    #0  0xb7f41e79 in PQstatus () from /usr/lib/libpq.so.4
    #1  0x08049705 in bande () at bande.c:258
    #2  0x0804998f in main () at bande.c:325

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    54
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2007
    Messages : 54
    Points : 43
    Points
    43
    Par défaut
    J'ai fait des tests en changeant de taille ma variable maRequete (et d'une autre chaine que je concatène à maRequete) et l'erreur intervient plus tard.

    La POO c'est tellement mieux

Discussions similaires

  1. Pb segmentation fault avec glutinit()
    Par pipistrelle dans le forum GLUT
    Réponses: 2
    Dernier message: 17/11/2004, 23h17
  2. [SDL_Image] Img_Load : segmentation fault ....
    Par Mathieu.J dans le forum OpenGL
    Réponses: 6
    Dernier message: 19/10/2004, 23h52
  3. [REDHAT] Segmentation fault systematique
    Par mela dans le forum RedHat / CentOS / Fedora
    Réponses: 2
    Dernier message: 21/09/2004, 06h05
  4. Réponses: 13
    Dernier message: 13/07/2004, 15h41
  5. Comment contrer la "segmentation fault" ?
    Par guillaume_pfr dans le forum C
    Réponses: 15
    Dernier message: 08/08/2003, 13h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo