Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, March 2002
memory leak


Message 1 in thread

Hi everybody,

I detected a memory leak in SQL.
I am using: SuSe 7.2  Qt3.0.0, and for connecting to the PostgreSql Database 
I use unixODBC.

Now the Problem:
I use QSqlQuery to get data from the database like this:

1 QString QueryResult;
2 QSqlQuery Target( "SELECT blabla... FROM blabla WHERE blabla..." );
3 if ( Target.isActive() ) {
4    while ( Target.next() ) {
5       // I know that the table has three columns 
6        for( int i = 0; i < 3; i++ ) {
7           QueryResult = QueryResult + Target.value( i ).toString();
8        }
9}

The table is very big. Now I traced the memory with top and I saw that the 
"memory used" grows and grows. and when I stop the request the memory will 
not be released. I also changed line 7 to: 
QueryResult = QueryResult + "hello world";
I traced it the same way. The "memory used" grown also but after stopping the 
request the memory has been released.
Is this a bug in Target.value(i) or am I doing something wrong?
If so please let me know.

Best regards,
Abud


Message 2 in thread

You allocate more and more memory yourself as the program runs. The 
variable QueryResult seems to contain all the values you read. This be 
really slow also as QuereResult grows.

/Mauritz
GlobeCom AB

Abdul-Rahman Masoud wrote:

>Hi everybody,
>
>I detected a memory leak in SQL.
>I am using: SuSe 7.2  Qt3.0.0, and for connecting to the PostgreSql Database 
>I use unixODBC.
>
>Now the Problem:
>I use QSqlQuery to get data from the database like this:
>
>1 QString QueryResult;
>2 QSqlQuery Target( "SELECT blabla... FROM blabla WHERE blabla..." );
>3 if ( Target.isActive() ) {
>4    while ( Target.next() ) {
>5       // I know that the table has three columns 
>6        for( int i = 0; i < 3; i++ ) {
>7           QueryResult = QueryResult + Target.value( i ).toString();
>8        }
>9}
>
>The table is very big. Now I traced the memory with top and I saw that the 
>"memory used" grows and grows. and when I stop the request the memory will 
>not be released. I also changed line 7 to: 
>QueryResult = QueryResult + "hello world";
>I traced it the same way. The "memory used" grown also but after stopping the 
>request the memory has been released.
>Is this a bug in Target.value(i) or am I doing something wrong?
>If so please let me know.
>
>Best regards,
>Abud
>
>--
>List archive and information: http://qt-interest.trolltech.com
>


Message 3 in thread

I will get specific:

Everytime I get a request to get something from the database a signal will be 
emitted, which is connected to this slot
void classname::slot_bla(.......) {
   QString QueryResult;
   QSqlQuery Target( "SELECT blabla... FROM blabla WHERE blabla..." );
   if ( Target.isActive() ) {
    while ( Target.next() ) {
     // I know that the table has three columns 
      for( int i = 0; i < 3; i++ ) {
         QueryResult = QueryResult + Target.value( i ).toString();
      }
    }
  ..........
  }
  .......
}
this means after leaving the slot all the variables are destroyed because  
they are localvariables.
But that does not happen here!
As I mentioned, I have only changed "Target.value(i).toStning" into a const 
QString "hello world" and there was no leak any more.