Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1550 connectés 

  FORUM HardWare.fr
  Programmation
  SQL/NoSQL

  PQExec et Segmentation Fault

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

PQExec et Segmentation Fault

n°1567961
SangJun
Posté le 31-05-2007 à 14:38:37  profilanswer
 

Bonjour,
 
Comment se fait que PgExec génère une erreur de segmentation pour une requête qui s'exécute "manuellement" si on la copie-colle sur PgAdmin?
 
J'ai beau retourner le problème dans tout les sens, je ne vois pas comment m'en sortir. Je copie-colle le code pour donner un peu plus de détails.
 
Merci de votre aide
 
 

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

mood
Publicité
Posté le 31-05-2007 à 14:38:37  profilanswer
 

n°1568517
SangJun
Posté le 01-06-2007 à 11:59:33  profilanswer
 

Après ce changement :
 

Code :
  1. strcpy(maRequete,"***une requete***" );
  2. fflush(stdout);
  3. printf("OK1\n" );
  4. if(PQstatus(conn) != CONNECTION_OK)
  5. {
  6. printf("PQstatus(conn) != CONNECTION_OK\n" );
  7. }
  8. printf("OK2\n",maRequete);
  9. res = PQexec(conn,maRequete); /* Instruction qui génère l'erreur de segmentation */


 
Le terminal m'affiche :
 

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

n°1568535
Taz
bisounours-codeur
Posté le 01-06-2007 à 12:13:54  profilanswer
 

tu voudrais pas commencer par corriger les warnings de compilation. Et le problème est à la ligne 258

n°1568567
SangJun
Posté le 01-06-2007 à 13:20:47  profilanswer
 

Pour les warnings, je vois pas trop où est le problème.
 
J'ai compris où est le problème (PQstatus(conn)) mais je ne vois pas comment le résoudre.

n°1568699
SangJun
Posté le 01-06-2007 à 16:00:21  profilanswer
 

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 :(


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  SQL/NoSQL

  PQExec et Segmentation Fault

 

Sujets relatifs
Segmentation Fault : bersoin d'aideErreur de segmentation avec un main vide
utilisation de mysql++ => Seg Fault !Segmentation couleur
segmentation fault sur producteur/consommateurAxis Fault exception
Segmentation Fault 
Plus de sujets relatifs à : PQExec et Segmentation Fault


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR