Bug #46949 memory leak with failed alter table to create partitions based on extract()
Submitted: 27 Aug 2009 8:05 Modified: 1 Dec 2010 11:25
Reporter: Shane Bester (Platinum Quality Contributor) Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: DDL Severity:S2 (Serious)
Version:5.1.39, 5.1, next bzr OS:Any
Assigned to:
Tags: EXTRACT, Leak
Triage: Triaged: D2 (Serious)

[27 Aug 2009 8:05] Shane Bester
Description:
68 bytes in 1 blocks are still reachable in loss record 4 of 7
at: malloc (vg_replace_malloc.c:149)
by: _mymalloc (safemalloc.c:137)
by: String::real_alloc(unsigned) (sql_string.cc:51)
by: String::alloc(unsigned) (sql_string.h:205)
by: Item_extract::fix_length_and_dec() (item_timefunc.cc:2269)
by: Item_func::fix_fields (THD*, Item**) (item_func.cc:198)
by: fix_fields_part_func (sql_partition.cc:978)
by: fix_partition_func (sql_partition.cc:1633)
by: open_table_from_share (table.cc:1828)
by: ha_create_table (handler.cc:3569)
by: rea_create_table (unireg.cc:417)
by: mysql_create_table_no_lock (sql_table.cc:3845)

presumably, this is never freed on error:

void Item_extract::fix_length_and_dec()
{
  value.alloc(32);				// alloc buffer
<cut>
}

How to repeat:
run mysqld under valgrind.  execute this:

drop table if exists `t1`;
create table `t1`(`a` int primary key,`b` int)engine=myisam;
alter table `t1` partition by hash(extract(month from `b`)) partitions 1;

shutdown and see valgrind report.
[27 Aug 2009 8:41] Sveta Smirnova
Thank you for the report.

Verified as described. Not repeatable with MTR running with --valgrind option.
[9 Mar 2010 14:02] Kristian Nielsen
This patch fixes the leak.

=== modified file 'sql/table.cc'
--- sql/table.cc        2010-03-04 08:03:07 +0000
+++ sql/table.cc        2010-03-09 12:44:30 +0000
@@ -1847,8 +1847,8 @@ int open_table_from_share(THD *thd, TABL
     {
       if (work_part_info_used)
         tmp= fix_partition_func(thd, outparam, is_create_table);
-      outparam->part_info->item_free_list= part_func_arena.free_list;
     }
+    outparam->part_info->item_free_list= part_func_arena.free_list;
 partititon_err:
     if (tmp)
     {
[15 Oct 2010 11:54] Libing Song
Bug#55506 is a duplicate of this bug.
[15 Oct 2010 11:56] Libing Song
See also Bug#56996
[1 Dec 2010 11:25] Mattias Jonsson
Duplicate of bug#56380. Using this patch to fix that bug.