Skip to content

Calling mysqli_stmt constructor leaks memory #8084

Closed

Description

@kamil-tekiela

Description

The following code:

<?php

$mysqli = new mysqli('localhost', 'user', 'password', 'test');

$stmt = $mysqli->prepare("SELECT 1");
$stmt->__construct($mysqli);

Resulted in this output:

[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_connection.c(685) :  Freeing 0x00007f0ba1c01188 (24 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(349) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.h(93) :  Freeing 0x00007f0ba1c011c0 (23 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(304) : Actual location (location was relayed)
Last leak repeated 3 times
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqli/mysqli_api.c(1885) :  Freeing 0x00007f0ba1c011f8 (24 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_auth.c(104) :  Freeing 0x00007f0ba1c01230 (21 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(94) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_connection.c(1565) :  Freeing 0x00007f0ba1c02780 (56 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(94) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/Zend/zend_string.h(150) :  Freeing 0x00007f0ba1c04870 (40 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
Last leak repeated 3 times
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_connection.c(687) :  Freeing 0x00007f0ba1c04a50 (34 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(349) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_commands.c(641) :  Freeing 0x00007f0ba1c5b6c0 (32 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(349) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_connection.c(1507) :  Freeing 0x00007f0ba1c5b780 (30 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(349) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/Zend/zend_hash.c(172) :  Freeing 0x00007f0ba1c6b480 (320 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqli/mysqli_api.c(1833) :  Freeing 0x00007f0ba1c780e0 (80 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(187) :  Freeing 0x00007f0ba1c78150 (72 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(117) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_statistics.c(218) :  Freeing 0x00007f0ba1c79600 (1304 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(110) :  Freeing 0x00007f0ba1c7c500 (1224 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(136) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_statistics.c(217) :  Freeing 0x00007f0ba1c830f0 (16 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(246) :  Freeing 0x00007f0ba1c87000 (376 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(136) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(193) :  Freeing 0x00007f0ba1c88000 (816 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(117) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(224) :  Freeing 0x00007f0ba1c8e140 (272 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(136) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(267) :  Freeing 0x00007f0ba1c8e280 (232 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(136) : Actual location (location was relayed)
=== Total 25 memory leaks detected ===

But I expected this output instead:

PHP Version

PHP 8.0, PHP 8.1, master

Operating System

No response

Activity

  1. cmb69 commented on Feb 14, 2022

    @cmb69
    Member

    Same here on Windows.

  2. cmb69 commented on Feb 17, 2022

    @cmb69
    Member

    At least one problem is that mysqli_stmt::__construct() allocates memory for the underlying structures, instead of allocation that memory in mysqli_objects_new(). So when the constructor is called manually, that memory leaks. I presume that other mysqli objects have the same issue. Given that calling the constructor manually is pretty uncommon, it might not be worth fixing this for any of the stable versions, but rather to target "master".

  3. added a commit that references this issue on Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions