pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedPQclear() causes coredump
3+ messages / 2 participants
[nested] [flat]
* PQclear() causes coredump
@ 2000-10-15 18:19 Oliver Fischer <nexus@fileseeker.net>
2000-10-15 20:25 ` Re: PQclear() causes coredump Alfred Perlstein <bright@wintelcom.net>
0 siblings, 1 reply; 3+ messages in thread
From: Oliver Fischer @ 2000-10-15 18:19 UTC (permalink / raw)
To: pgsql-interfaces
Hi Folks,
I write a program in c++ using PostgreSQL 7.0 at the moment.
During this, I have
written some small classes for accessing the database. One of
this classes is QtResultSet. I use them to manage my statments.
To avoid memory leaks, I use the dtor to free the resultset with
PQclear(). Unfortunaly every call to PQclear causes a coredump.
What is my mistake?
=== QtResultSet.h ===
#include <string>
#include <map>
#include <vector>
#include <libpq-fe.h>
#include "../errors/QtExceptions.h"
#ifndef __QTRESULTSET__
#define __QTRESULTSET__
namespace qt {
namespace database {
using namespace std;
using namespace qt::errors;
class QtResultSet {
public:
virtual ~QtResultSet();
vector<string> fieldnames() throw(QtDbConnectionBadErr,
QtDbFatalErr,
QtDbNonfatalErr, QtDbBadResponseErr);
vector<string> fetch(int) throw(QtDbConnectionBadErr,
QtDbFatalErr,
QtDbNonfatalErr, QtDbBadResponseErr);
vector<string> fetch(void) throw(QtDbConnectionBadErr,
QtDbFatalErr,
QtDbNonfatalErr, QtDbBadResponseErr);
string get_error();
int rows();
int fields();
void execute() throw(QtDbConnectionBadErr, QtDbFatalErr,
QtDbNonfatalErr, QtDbBadResponseErr);
friend class QtConnection;
protected:
QtResultSet(PGconn *, const string &);
private:
void fetch_row(int, vector<string>*);
bool binarydata_m;
int rows_m;
int fields_m;
int currentrow_m;
string query_m;
string error_m;
PGresult *pgresult_mp;
PGconn *pgconn_mp;
};
} }
#endif
=== QtResultSet.cpp ===
#include <string>
#include <libpq-fe.h>
#include "QtResultSet.h"
#include "../errors/QtExceptions.h"
namespace qt {
namespace database {
using namespace std;
using namespace qt::errors;
QtResultSet::~QtResultSet() {
PQclear(this->pgresult_mp);
}
QtResultSet::QtResultSet(PGconn *pgconn, const string &query)
{
pgconn_mp = pgconn;
query_m = query;
fields_m = 0;
rows_m = 0;
currentrow_m = 0;
}
void QtResultSet::execute() throw(QtDbConnectionBadErr,
QtDbFatalErr,
QtDbNonfatalErr, QtDbBadResponseErr)
try {
pgresult_mp = PQexec(pgconn_mp, query_m.c_str());
ExecStatusType res = PQresultStatus(pgresult_mp);
if (res == (PGRES_COMMAND_OK || PGRES_TUPLES_OK)) {
rows_m = PQntuples(pgresult_mp);
fields_m = PQnfields(pgresult_mp);
binarydata_m = PQbinaryTuples(pgresult_mp);
currentrow_m = -1;
}
else {
error_m = PQresultErrorMessage(pgresult_mp);
switch (res) {
case PGRES_EMPTY_QUERY:
break;
case PGRES_BAD_RESPONSE:
throw QtDbBadResponseErr();
break;
case PGRES_NONFATAL_ERROR:
throw QtDbNonfatalErr();
break;
case PGRES_FATAL_ERROR:
throw QtDbFatalErr();
break;
}
}
}
catch (...) {
#ifdef QTDEBUG
cerr << "Exception catched and PQclear() called." << endl;
#endif QTDEBUG
PQclear(pgresult_mp);
#ifdef QTDEBUG
cerr << "Exception rethrown." << endl;
#endif QTDEBUG
throw;
} }
int QtResultSet::fields() {
return fields_m;
}
int QtResultSet::rows() {
return rows_m;
}
vector<string> QtResultSet::fieldnames()
throw(QtDbConnectionBadErr, QtDbFatalErr,
QtDbNonfatalErr, QtDbBadResponseErr) {
vector<string> row;
for (int i = 0; i < rows_m; i++) {
row.push_back(PQfname(pgresult_mp, i));
}
return row;
}
vector<string> QtResultSet::fetch(void)
throw(QtDbConnectionBadErr, QtDbFatalErr,
QtDbNonfatalErr, QtDbBadResponseErr) {
vector<string> row;
if (currentrow_m < (rows_m - 1)) {
this->fetch_row(++currentrow_m, &row);
}
else {
}
return row;
}
vector<string> QtResultSet::fetch(int id)
throw(QtDbConnectionBadErr, QtDbFatalErr,
QtDbNonfatalErr, QtDbBadResponseErr) {
vector<string> row;
if ((id > 0) && (id < rows_m)) {
this->fetch_row(id, &row);
}
else {
}
return row;
}
void QtResultSet::fetch_row(int rowid, vector<string>
*rowdata) {
for (int i = 0; i < fields_m; i++) {
rowdata->push_back(string("Aber Hallo"));
}
}
string QtResultSet::get_error() {
return this->error_m;
} // QtResultSet::get_error
} // namespace database
} // namespace qt
BYe,
Oliver
--
Oliver Fischer
Level of Detail, Brunnenstr. 196, D-10119 Berlin
Tel. +49/30/280 491 43
--
Oliver Fischer
Level of Detail, Brunnenstr. 196, D-10119 Berlin
Tel. +49/30/280 491 43
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: PQclear() causes coredump
2000-10-15 18:19 PQclear() causes coredump Oliver Fischer <nexus@fileseeker.net>
@ 2000-10-15 20:25 ` Alfred Perlstein <bright@wintelcom.net>
2000-10-16 06:48 ` Re: PQclear() causes coredump Oliver Fischer <nexus@fileseeker.net>
0 siblings, 1 reply; 3+ messages in thread
From: Alfred Perlstein @ 2000-10-15 20:25 UTC (permalink / raw)
To: Oliver Fischer <nexus@fileseeker.net>; +Cc: pgsql-interfaces
* Oliver Fischer <nexus@fileseeker.net> [001015 11:19] wrote:
> Hi Folks,
>
> I write a program in c++ using PostgreSQL 7.0 at the moment.
> During this, I have
> written some small classes for accessing the database. One of
> this classes is QtResultSet. I use them to manage my statments.
>
> To avoid memory leaks, I use the dtor to free the resultset with
> PQclear(). Unfortunaly every call to PQclear causes a coredump.
> What is my mistake?
>
> === QtResultSet.h ===
[snip]
Perhaps you can post us a traceback from your coredump? If you could
also give a relatively simple way to reproduce the bug with a driver
program that uses your class it would be helpful.
-Alfred
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: PQclear() causes coredump
2000-10-15 18:19 PQclear() causes coredump Oliver Fischer <nexus@fileseeker.net>
2000-10-15 20:25 ` Re: PQclear() causes coredump Alfred Perlstein <bright@wintelcom.net>
@ 2000-10-16 06:48 ` Oliver Fischer <nexus@fileseeker.net>
0 siblings, 0 replies; 3+ messages in thread
From: Oliver Fischer @ 2000-10-16 06:48 UTC (permalink / raw)
To: Alfred Perlstein <bright@wintelcom.net>; +Cc: pgsql-interfaces
Hy,
I found the bug this night. The reason was the call to PQclear()
in the destructor of my class. In some places I used a assingment
like:
QtResultSet obj = otherQtResultSetObj;
So, the destruktor of otherQtResultObj was called. The coredump
occured within obj when PQclaer was called another time.
Byem
Oliver
----- Original Message -----
From: "Alfred Perlstein" <bright@wintelcom.net>
To: "Oliver Fischer" <nexus@fileseeker.net>
Cc: <pgsql-interfaces@postgresql.org>
Sent: Sunday, October 15, 2000 10:25 PM
Subject: Re: [INTERFACES] PQclear() causes coredump
> * Oliver Fischer <nexus@fileseeker.net> [001015 11:19] wrote:
> > Hi Folks,
> >
> > I write a program in c++ using PostgreSQL 7.0 at the moment.
> > During this, I have
> > written some small classes for accessing the database. One of
> > this classes is QtResultSet. I use them to manage my
statments.
> >
> > To avoid memory leaks, I use the dtor to free the resultset
with
> > PQclear(). Unfortunaly every call to PQclear causes a
coredump.
> > What is my mistake?
> >
> > === QtResultSet.h ===
>
> [snip]
>
> Perhaps you can post us a traceback from your coredump? If you
could
> also give a relatively simple way to reproduce the bug with a
driver
> program that uses your class it would be helpful.
>
> -Alfred
>
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2000-10-16 06:48 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-10-15 18:19 PQclear() causes coredump Oliver Fischer <nexus@fileseeker.net>
2000-10-15 20:25 ` Alfred Perlstein <bright@wintelcom.net>
2000-10-16 06:48 ` Oliver Fischer <nexus@fileseeker.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox