memory leak of PQmakeEmptyPGresult??

Started by Annover 21 years ago3 messagesgeneral
Jump to latest
#1Ann
he629@yahoo.com.cn

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锟斤拷锟斤拷眩锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟�
锟斤拷女锟斤拷锟斤拷应锟叫撅拷锟叫o拷锟窖憋拷锟斤拷图锟斤拷锟斤拷图锟酵匡拷图
1G锟斤拷锟斤拷1000锟阶o拷锟脚伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟捷o拷

#2Ann
he629@yahoo.com.cn
In reply to: Ann (#1)
Re: memory leak of PQmakeEmptyPGresult??

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

xiaoling he <he629@yahoo.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锟斤拷锟斤拷眩锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟�
锟斤拷女锟斤拷锟斤拷应锟叫撅拷锟叫o拷锟窖憋拷锟斤拷图锟斤拷锟斤拷图锟酵匡拷图
1G锟斤拷锟斤拷1000锟阶o拷锟脚伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟捷o拷

---------------------------------
Do You Yahoo!?
150锟斤拷锟斤拷MP3锟斤拷锟斤拷眩锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟�
锟斤拷女锟斤拷锟斤拷应锟叫撅拷锟叫o拷锟窖憋拷锟斤拷图锟斤拷锟斤拷图锟酵匡拷图
1G锟斤拷锟斤拷1000锟阶o拷锟脚伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟捷o拷

#3Gaetano Mendola
mendola@bigfoot.com
In reply to: Ann (#2)
Re: memory leak of PQmakeEmptyPGresult??

Ann wrote:

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

Why then don't you share it ?

Regards
Gaetano Mendola