Hi,

I'm trying to read a set of data from a psql table, then create a new object for each row.
Sounds easy. But...

Here's the code I use :

class CSpell() {
 public:
   CSpell(){};
   virtual ~CSpell(){};
}

bool loadSpells( pqxx::transaction_base& t ){
 CSpell* pSpell = NULL;
 std::string sqlQuery = "SELECT spell_id FROM tbl_game_spell";
 pqxx::result R = t.exec( sqlQuery );
 for (pqxx::result::const_iterator it = R.begin(); it != R.end(); it++) {
pSpell = new CSpell(); // memory leak, I know. This pointer will be stored somewhere later.
 }
 return true;
}

When compiling and running this, the loadSpells function executes fine and returns true.
But a few seconds after, I get a Segmentation Fault signal.
If I don't call this function in my code, everything's fine (I can let the software run for hours, without a single problem).

I tried to isolate the problem, and finally, I tried this in the function body :

{
 CSpell* pSpell = NULL;
 std::string sqlQuery = "SELECT spell_id FROM tbl_game_spell";
 pqxx::result R = t.exec( sqlQuery );

 for (int i  = 0; i < 48; i++)
pSpell = new CSpell(); // memory leak, I know. This pointer will be stored somewhere later.
 return true;
}

Here, 48 is the number of rows in the tbl_game_spell table.
This code generates the same Segmentation Fault problem.

I then tried :
{
 CSpell* pSpell = NULL;

 for (int i  = 0; i < 48; i++)
pSpell = new CSpell(); // memory leak, I know. This pointer will be stored somewhere later.
 std::string sqlQuery = "SELECT spell_id FROM tbl_game_spell";
 pqxx::result R = t.exec( sqlQuery );
 return true;
}

And it works. Well... I mean : it doesn't crash. But of course, It's not usable in my program.

Finally, I tried this :
{
 CSpell* pSpell = NULL;

 std::string sqlQuery = "SELECT spell_id FROM tbl_game_spell";
 {
// object result in brackets, in order to force deletion before to do the loop pqxx::result R = t.exec( sqlQuery ); }
 for (int i  = 0; i < 48; i++)
pSpell = new CSpell(); // memory leak, I know. This pointer will be stored somewhere later.
 return true;
}

And this... worked.
At this time, I don't know anymore where to look.
Can it be a namespace problem ? A heap size problem ? A libpqxx problem ?

Thanks a lot in advance for your help, and sorry for my crap english...

Camille Chafer

_______________________________________________
Libpqxx-general mailing list
[email protected]
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general

Reply via email to