Bug #19842 problems with static allocation of MYSQL structure
Submitted: 16 May 2006 9:42 Modified: 18 Jun 2006 14:32
Reporter: Vladimir Kolesnikov Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S2 (Serious)
Version:5.0.21 OS:Windows (windows xp sp2)
Assigned to: Assigned Account CPU Architecture:Any

[16 May 2006 9:42] Vladimir Kolesnikov
Description:
The following code gives a crash (libs and headers are from mysql official package 5.0.21 for windows).

#include <my_global.h>
#include <mysql.h>

class A {
 MYSQL mysql;
public:
 A();
};

A::A()
{
 mysql_init(&mysql);
}

int main(int argc, char* argv[])
{
 A a;
 A b;
} 

the crash happens because of stack corruption after mysql_init(). Dynamic allocation (i.e. MYSQL *m= mysql_init(NULL) ) works ok.

PS

Citation from the docs:

MYSQL *mysql_init(MYSQL *mysql) 

Description 

Allocates or initializes a MYSQL object suitable for mysql_real_connect(). If mysql is a NULL pointer, the function allocates, initializes, and returns a new object. Otherwise, the object is initialized and the address of the object is returned.

How to repeat:
Compile the sample using either MSVC or BDS and run it - you'll get either a crash (in a release build) or a stack corruption warning (in debug build)
[17 May 2006 16:03] MySQL Verification Team
Thank you for the bug report. I was unable to repeat compiling with
current source 5.0.22, against mysqlclient.lib and using VS 2003.

Could you please provide which library version did you linked and which
compiler you had used.

Thanks in advance.
[18 May 2006 13:11] Vladimir Kolesnikov
Miguel, as I already mentioned I used library and header files from the official 5.0.21 package for windows (choose install option to install developer components). Compiler MSVC 2003. 

Please let me know if this info is not enough.
[18 May 2006 13:33] Vladimir Kolesnikov
Miguel,

Looks like we used different libraries - I used libmysql.lib to link to the dll version. Sorry, didnt have time to check with static version (dont get confused with "static" in the applied archive name)

Btw this is what I found in docs:

One known problem with Borland C++ is that it uses a different structure alignment than VC++. This means that you run into problems if you try to use the default libmysql.dll libraries (that were compiled using VC++) with Borland C++. To avoid this problem, only call mysql_init() with NULL as an argument, not a pre-allocated MYSQL structure. 

This is very similar to my problem but it happens on MSVC as well.
[18 May 2006 14:28] MySQL Verification Team
Solution Test

Attachment: bug19842.zip (application/x-zip-compressed, text), 5.85 KiB.

[18 May 2006 14:32] MySQL Verification Team
Thank you for the feedback. I created a project and linked against the
static library for to use libmysql.dll and I was'nt able to repeat, see
attached file of the solution. Then I looked your project and you compiled
with Runtime Library Single-threaded Debug (/MLd), you should use the
Multi-threaded Debug (/MTd).

Test my solution, you need to expand the zip file in the install root
directory.

Thanks in advance.
[18 Jun 2006 23: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".