dbTalk Databases Forums  

memory leak of PQmakeEmptyPGresult??

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss memory leak of PQmakeEmptyPGresult?? in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
xiaoling he
 
Posts: n/a

Default memory leak of PQmakeEmptyPGresult?? - 10-12-2004 , 08:09 PM






I try to detect potential memory management bugs of my program with valgrind. (PostgreSQL is at version 8.0 beta2. Operating System is Red Hat Enterprise Linux 3. Valgrind is at version 2.2.0.)

  After Program terminated, Valgrind reports a memory lost error information as follows:
==13524== 208 bytes in 2 blocks are definitely lost in loss record 4 of 12
==13524== at 0x1B903D1C: malloc (vg_replace_malloc.c:131)
==13524== by 0x1B95C8B2: PQmakeEmptyPGresult (in /usr/lib/libpq.so.3.0)
==13524== by 0x1B95D14A: (within /usr/lib/libpq.so.3.0)
==13524== by 0x1B95D963: PQgetResult (in /usr/lib/libpq.so.3.0)
  
I never call PQmakeEmptyPGresult and PQgetResult function in my program!

Then i searched information about PQmakeEmptyPGresult:
  “PQmakeEmptyPGresult Constructs an empty PGresult object with the given status.it is libpq’s internal routine to allocate and initialize an empty PGresult object. It is exported because some applications find it useful to generate result objects (particularly objects with error status)themselves. If conn is not NULL and status indicates an error, the connection’s current error message is copied into the PGresult. Note that PQclear should eventually be called on the object, just as with a PGresult returned by libpq itself. ”

  It seems some connection errors happened at the time I do some query or update operations on database.

Now I was puzzled, because program runs normally and returns correct result every time. And I am sure I called PQclear()
after everytime called PQexec().

  There is a example of my program:
Bool bTest(void)
{
  PGresult *res;
  char acQryBuf[200];
sprintf(acQryBuf, "%s", "select count(*) from testable ");
res =PQexec(conn,acQryBuf);
if ( PQresultStatus(res)!= PGRES_COMMAND_OK){
printf("\n %s\n",PQresultErrorMessage(res));
PQclear(res);
return false;
}
PQclear(res);
return true;
}

If PQclear() don't clear PGresult successfully, there will be some memory leaks.....Is it right? Why PQclear () run unsuccessfully evrytime?

Would that have anything to do with it? Any other ideas of things to try?
Thanks!
Ann
2004.10.12 pm 17:00



---------------------------------
Do You Yahoo!?
150万曲MP3疯狂搜,带您闯入音乐殿堂
美女明星应有尽有,搜遍美图、艳图和酷图
1G就是1000兆,雅虎电邮自助扩容!

Reply With Quote
  #2  
Old   
Ann
 
Posts: n/a

Default Re: memory leak of PQmakeEmptyPGresult?? - 10-12-2004 , 10:26 PM






I found the reason of this question and fixed the bug )

xiaoling he <he629 (AT) yahoo (DOT) com.cn> wrote:I try to detect potential memory management bugs of my program with valgrind. (PostgreSQL is at version 8.0 beta2. Operating System is Red Hat Enterprise Linux 3. Valgrind is at version 2.2.0.)

  After Program terminated, Valgrind reports a memory lost error information as follows:
==13524== 208 bytes in 2 blocks are definitely lost in loss record 4 of 12
==13524== at 0x1B903D1C: malloc (vg_replace_malloc.c:131)
==13524== by 0x1B95C8B2: PQmakeEmptyPGresult (in /usr/lib/libpq.so.3.0)
==13524== by 0x1B95D14A: (within /usr/lib/libpq.so.3.0)
==13524== by 0x1B95D963: PQgetResult (in /usr/lib/libpq.so.3.0)
  
I never call PQmakeEmptyPGresult and PQgetResult function in my program!

Then i searched information about PQmakeEmptyPGresult:
  “PQmakeEmptyPGresult Constructs an empty PGresult object with the given status.it is libpq’s internal routine to allocate and initialize an empty PGresult object. It is exported because some applications find it useful to generate result objects (particularly objects with error status)themselves. If conn is not NULL and status indicates an error, the connection’s current error message is copied into the PGresult. Note that PQclear should eventually be called on the object, just as with a PGresult returned by libpq itself. ”

  It seems some connection errors happened at the time I do some query or update operations on database.

Now I was puzzled, because program runs normally and returns correct result every time. And I am sure I called PQclear()
after everytime called PQexec().

  There is a example of my program:
Bool bTest(void)
{
  PGresult *res;
  char acQryBuf[200];
sprintf(acQryBuf, "%s", "select count(*) from testable ");
res =PQexec(conn,acQryBuf);
if ( PQresultStatus(res)!= PGRES_COMMAND_OK){
printf("\n %s\n",PQresultErrorMessage(res));
PQclear(res);
return false;
}
PQclear(res);
return true;
}

If PQclear() don't clear PGresult successfully, there will be some memory leaks.....Is it right? Why PQclear () run unsuccessfully evrytime?

Would that have anything to do with it? Any other ideas of things to try?
Thanks!
Ann
2004.10.12 pm 17:00



---------------------------------
Do You Yahoo!?
150万曲MP3疯狂搜,带您闯入音乐殿堂
美女明星应有尽有,搜遍美图、艳图和酷图
1G就是1000兆,雅虎电邮自助扩容!



---------------------------------
Do You Yahoo!?
150万曲MP3疯狂搜,带您闯入音乐殿堂
美女明星应有尽有,搜遍美图、艳图和酷图
1G就是1000兆,雅虎电邮自助扩容!

Reply With Quote
  #3  
Old   
Gaetano Mendola
 
Posts: n/a

Default Re: memory leak of PQmakeEmptyPGresult?? - 10-13-2004 , 08:37 AM



Ann wrote:
Quote:
I found the reason of this question and fixed the bug )

Why then don't you share it ?



Regards
Gaetano Mendola



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.