Bug #29863 LIBMYSQL.DLL: 0xC0000005: Access Violation
Submitted: 18 Jul 2007 7:51 Modified: 6 Jul 2010 8:16
Reporter: Marius Roibu Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S3 (Non-critical)
Version:5.0.45 OS:Windows (XP SP2)
Assigned to: CPU Architecture:Any
Tags: reconnect failure

[18 Jul 2007 7:51] Marius Roibu
Description:
I wrote a small C program what inserts several values into a database. From time to time this program hits me with "MySQL server has gone away"... In this case I attempt to reconnect (by I don't know what reason reconnect from MYSQL structure seems to be ignored).
It goes several times thru reconnect function and once it crashes). I captured the message and it it looks as follow:

Trying to reconnect to server...
Error: Freeing pointer out of range at line 231, '\build\mysql-5.0.45-winbuild\m
ysql-community-nt-5.0.45-build\vio\vio.c'
Error: Freeing unallocated data at line 232, '\build\mysql-5.0.45-winbuild\mysql
-community-nt-5.0.45-build\vio\vio.c'
Trying to reconnect to server...
Connecting to Mysql server(localhost - 1, 940, 4937024) Attempt: 9...

On debug on VC6 an message with "LIBMYSQL.DLL: 0xC0000005: Access Violation" pops up.
It is multi threading exe and it uses semaphores to limit access of other threads to mysql during sql operation, including reconnect time...
I use an AMD x2 64bits machine with 2 GB RAM if that counts...

How to repeat:
On connect:
int sqlConnect(char* hostname, char* username, char* password, char* table,MYSQL* rMysql, unsigned int port)
{
	my_bool  my_true = TRUE;
	rMysql=mysql_init(rMysql);
	mysql_options(rMysql,MYSQL_OPT_COMPRESS,0);
        (*rMysql).reconnect=1;
	if (!mysql_real_connect(rMysql,hostname,username,password,table,port,NULL,0))
	{
		return 0;

	}
	(*rMysql).reconnect=1;
	return 1;
}

int my_mysql_query(MYSQL*mysql, char* sqlQuery,int iMutex)
{
int ret;

	thrdBlock(iMutex);
	ret=mysql_real_query(mysql, sqlQuery,strlen(sqlQuery));
	if(ret!=0)
	{
		if(iMutex==BLOCKDB1)
			my_mysql_reConnect(MYSQLSERVER1, USERDB1, PASSDB1, DB1,&gMysqlDB1, MYSQLSERVER_PORT1, BLOCKDB1);
		else
		if(iMutex==BLOCKINDEX)
			my_mysql_reConnect(MYSQLSERVER2, USERDB2, PASSDB2, DB2,&gMysqlDB2, MYSQLSERVER_PORT2, BLOCKINDEX);
	ret=mysql_real_query(mysql, sqlQuery,strlen(sqlQuery));
	}
	thrdUnBlock(iMutex);

return ret;
}

reconnect function:
int my_mysql_reConnect(char* hostname, char* username, char* password, char* table,MYSQL* rMysql, unsigned int port, int iMutex)
{
varReconn++;
	printf("Trying to reconnect to server...\r\n");
	printf("Connecting to Mysql server(%s - %i, %i, %i) Attempt: %i...\r\n",hostname, iMutex, sizeof(*rMysql), (int)rMysql, varReconn);
	//thrdBlock(iMutex);
	mysql_options(rMysql,MYSQL_OPT_COMPRESS,0);
	if (!mysql_real_connect(rMysql,hostname,username,password,table,port,NULL,0))
	{printf("ERROR");
		printf(": %s\r\n", mysql_error(rMysql));
		iQuit=1;		
		//thrdUnBlock(iMutex);
		ERROR_LOG(mysql_error(rMysql))			
		return 0;
	}
	printf("OK\r\n");
return 1;
}
[18 Jul 2007 8:46] Sveta Smirnova
Thank you for the report.

>I don't know what reason reconnect from MYSQL structure seems to be ignored)

Please try to set this option as described in our manual at http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html and if you can repeat described behaviour please provide server error log and configuration file.
[18 Jul 2007 9:22] Marius Roibu
I tried two approaches in this issue of restarting with autoconnect:
(*rMysql).reconnect=1;
and

my_bool  my_true = TRUE;
mysql_options(rMysql, MYSQL_OPT_RECONNECT, &my_true);

No error generated during compilation neither any help...
[18 Jul 2007 9:34] Sveta Smirnova
Thank you for the feedback.

Please provide server error log and configuration file.
[18 Jul 2007 9:48] Marius Roibu
Content of my.ini as bellow:

[client]

port=3306

[mysql]

default-character-set=latin1

[mysqld]

# The TCP/IP Port the MySQL Server will listen on
port=3306

basedir="C:/Program Files/MySQL/MySQL Server 5.0/"

datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/"

default-character-set=latin1

default-storage-engine=INNODB

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

max_connections=100

query_cache_size=0

table_cache=256

tmp_table_size=18M

thread_cache_size=8

myisam_max_sort_file_size=100G

myisam_max_extra_sort_file_size=100G

myisam_sort_buffer_size=35M

key_buffer_size=25M

read_buffer_size=64K
read_rnd_buffer_size=256K

sort_buffer_size=256K

innodb_additional_mem_pool_size=2M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=1M
innodb_buffer_pool_size=47M
innodb_log_file_size=24M
innodb_thread_concurrency=8

these are the last entries:
070718  0:03:57  InnoDB: Starting shutdown...
070718  0:03:59  InnoDB: Shutdown completed; log sequence number 0 43655
070718  0:03:59 [Note] C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld-nt: Shutdown complete

070718  0:03:59  InnoDB: Started; log sequence number 0 43655
070718  0:03:59 [Note] C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld-nt: ready for connections.
Version: '5.0.45-community-nt'  socket: ''  port: 3306  MySQL Community Edition (GPL)

None of them coresponds as timming with time of crashing...
[18 Jul 2007 14:40] Marius Roibu
On debug session it shows this:
call        dword ptr [eax+6Ch] at different mem address.
Hope it helps...
[19 Jul 2007 13:35] Sveta Smirnova
Thank you for the feedback.

Could you please provide complete test program to we can try it in our environment. Also please indicate version of Visual Studio you use.
[19 Jul 2007 16:42] Marius Roibu
complete test program in order to reproduce libmysql.dll access violation

Attachment: bug-data-29863(ows7).exe (application/octet-stream, text), 409.25 KiB.

[19 Jul 2007 16:44] Marius Roibu
I still use Visual Studio 6.0...
[20 Jul 2007 9:44] Sveta Smirnova
Thank you for the feedback.

But we need source code to be sure problem is MySQL and not custom program. Please provide source code for your test program. You can make this attachment private.
[20 Jul 2007 11:23] Sveta Smirnova
Additional comment:

we don't support Visual Studio 6, so we must check problem with VS2005
[20 Jul 2007 13:55] Marius Roibu
That is source code, wrapped as self extracting archive...

Aditionally I've tested same program with same software configuration (WIN xp SP2, latest mysql distribution (ran on same machine) and I got no "MySQL server has gone away", and no crush for 20 hours.
Main difference was the machine: It is a PII (366MHz) with 256 MB RAM...
[7 Aug 2007 16:21] MySQL Verification Team
VS opening solution

Attachment: VS-corrupt-file.png (image/png, text), 52.43 KiB.

[7 Aug 2007 16:24] MySQL Verification Team
Thank you for the feedback. I tried to compile the test case however
with VS2003/2005 the project/solution file is corrupted. I noticed
an *.sln file are you able to test with VS2003/2005 compilers?.
Thanks in advance.
[17 Aug 2007 13:10] Marius Roibu
I will try to recompile on VC++ Express Edition soon after weekend...
[17 Aug 2007 13:20] MySQL Verification Team
Thank you for the feedback, please comment here the result with VS2005.
Thanks in advance.
[20 Aug 2007 12:25] Marius Roibu
updated source file

Attachment: bug-data-29863(ows7).rar (application/octet-stream, text), 496.63 KiB.

[29 Aug 2007 22:02] Sveta Smirnova
Thank you for the feedback.

I can not repeat described behaviour if compile using Visual Studio 2005. Can you repeat the error with VC Express Edition?
[30 Aug 2007 3:49] Marius Roibu
Sveta, seems to be platform dependent. I tried this app on P4 and it has no problem. Please use if you have the change AMD x2 to reproduce the behaviour...
[1 Oct 2007 19:47] Tonci Grgin
Marius, I see you're linking against several libraries, even odbc ones, with /MTd switch. I think you should make a test case which is as small as possible exhibiting this problem. During last year or so I had no such problems with AMD x64 and WinXP Pro.
[2 Nov 2007 0:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[6 Jul 2010 8:16] Sveta Smirnova
Thank you for the feedback.

3 of us were not able to repeat the problem. Regarding to AMD one of our engineers who has AMD box found you mix 32-bit and 64-bit compile options and libraries. Therefore I set status to "Can't repeat"