php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41871 mysql_free_result does not free result for a referenced result variable
Submitted: 2007-07-02 13:12 UTC Modified: 2007-07-03 10:04 UTC
From: kulakov74 at yandex dot ru Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.2.3 OS: Linux / Windows
Private report: No CVE-ID: None
 [2007-07-02 13:12 UTC] kulakov74 at yandex dot ru
Description:
------------
When I call mysql_free_result($Res) I expect I won't be able to use the result in calls to mysql_fetch_array() etc. But if $Res is passed by reference as a function parameter and is copied to another variable the result is not freed and I can still use it. 

I realize this is a VERY unlikely condition and I found it by accident (mixed 2 lines of code and got surprised to see no error). Also this might well be intentional behavior. Anyway, just in case. 

Reproduce code:
---------------
a($Res);

function a(&$Res){
$Res=mysql_query("Show Tables"); $Res1=$Res;
mysql_free_result($Res);
mysql_fetch_array($Res); //still works!
}

Expected result:
----------------
Warning: mysql_fetch_array(): 7 is not a valid MySQL result resource in

Actual result:
--------------
No errors

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-02 23:11 UTC] sniper@php.net
This is expected result. mysql_free_result() just decreases the refcount to the result and actual freeing happens during request shutdown (if refcount to the result is 0!).
 [2007-07-03 10:01 UTC] kulakov74 at yandex dot ru
Well maybe, but I checked MySql API mysql_free_result() source code and there's no reference counting logic, it just frees stuff. I suppose this is PHP's mysql_free_result() implementation that checks reference count, and it was intended as a kind of service to prevent people from losing a result set while there is still another reference around. But I do not think this is really expected and people use it this way by calling mysql_free_result() first and then using the same result again from another variable. I think most people expect PHP mysql_free_result() to call MySql mysql_free_result directly whatsoever. 

Also, I suppose PHP might call mysql_free_result() automatically when reference count goes to 0 even before the script is over, for example if I execute an Sql-query inside a function and then return from it without saving the result PHP might call mysql_free_result() when destroying the variable. 

But it is not obvious that reference count check should be done at direct calls to mysql_free_result() as then I say I want it, no matter how many references I still have. Also, it is not clear which exactly variable is dereferenced from the result set as the variable passed to mysql_free_result() still exists after the call and I can use it again. If I call mysql_free_result() with the same variable again it does free the result :)
 [2007-07-03 10:04 UTC] kulakov74 at yandex dot ru
A clarification: by saying "PHP might call mysql_free_result() automatically" I do not mean a feature request, I mean I suppose PHP already does so.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 08:01:28 2024 UTC