Question

This code looks like using all the system memory. Why does it happend?

sql::Statement *Query;
sql::ResulSet *Result;
while(1){
   Query = con->createStatement();
   Result = Query->executeQuery("SELECT `Some` FROM `Table` LIMIT 1");
   Result->next();
   Result->close(); 
   Query->close();
   cout << "console message..." << endl;
   //delete Query; SEGFAULT
}

If i comment all the lines except the cout the memory doesn't get filled. But using the SQL looks like Query = con->createStatement; is not replacing the old Query value and Result = bla; is not replacing the old value

Was it helpful?

Solution 2

Well it looks like there is no info on google. I found what was the problem.

as SJuan76 said each call to createStatement and to executeQuery is a new object

So i started to do a lot of tryes and i figure out of the following

  1. Use createStatement just once
  2. Before deleting Result check if it's open and close
  3. Delete Respuesta
  4. Only delete con and Query at the end of the program auto_ptr(is your friend sometimes)

So the code to get the "forever running" program to use always the same memory shoud look like this

sql::Statement *Query;
sql::ResulSet *Result;
Query = con->createStatement();
while(1){
 if(!Result->isClosed()) Result->close();
 delete Result;
 Result = Query->executeQuery("SELECT `Some` FROM `Table` LIMIT 1");
 Result->next();
 cout << "console message..." << endl;
}

OTHER TIPS

createStatement() and executeQuery are creating new objects (which are the ones you store in Query and Result. Unless you explicitly delete them, they will remain in memory.

Add

  delete Result;
  delete Query;

To your loop.

BTW, this is in the connector documentation (together with sample code).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top