MySQL NDB API AccessViolationException
MySQL NDB API AccessViolationException
我遇到了一个有趣的问题,该问题仅在 Windows 上发生,而在 Linux/Mono 上一切正常。我围绕 MySQL Cluster NDB API 库构建了一个 C++ 包装器,我通过 P/Invoke 从 C# 代码调用它。到目前为止我有三种方法:
init()
初始化环境和 returns 句柄
read()
使用handle 从数据库中读取数据
release()
释放资源
init()
在每个设置中都可以正常工作。但是,read()
抛出 AccessViolationException
,但仅在 Windows 上并且仅在一个线程中调用 init()
而在另一个线程中调用 read()
的情况下。如果我们在一个线程中做所有事情,那就行得通了!任何有关可能原因的想法将不胜感激。
更新 12/4/2016 - 在一个简单的 C++ 测试程序中注意到了相同的行为,其中 init()
在一个线程中被调用并且 read()
在另一个。因此,错误与 C#/C++ 互操作无关,它是 NDB API 库中的东西,可以下载 here (lib
文件夹,mysqlclient.lib
和 ndbclient_static.lib
).
C#代码:
private const string ndbapi = "ndb";
[DllImport(ndbapi)]
public extern static IntPtr init(IntPtr conn_string);
[DllImport(ndbapi)]
public extern static void read(IntPtr ndb_cluster_conn);
[DllImport(ndbapi)]
public extern static void release(IntPtr ndb_cluster_conn, IntPtr char_arr_ptr);
private static IntPtr handle;
private static void InitNdb() {
unsafe {
fixed (byte* conn_buf = Encoding.UTF8.GetBytes("node1:1186")) {
handle = ndb_api_init(new IntPtr(conn_buf));
}
}
}
static void Main(string[] args) {
Thread t = new Thread(InitNdb);// IF I CALL InitNDB IN THE SAME THREAD, EVERYTHING WORKS
t.Start();
.. //waiting for Thread t to complete...
IntPtr bytes_read = read(handle);
...
}
C++ 代码:(基于 official documentation 中的示例):
#if defined(WIN32)
#define DLLEXPORT __declspec(dllexport)
#define CALLCV __stdcall
#else
#define DLLEXPORT __attribute__((visibility("default")))
#define CALLCV __attribute__((cdecl))
#endif
..
extern "C" DLLEXPORT Ndb_cluster_connection* CALLCV init(char* connection_string) {
ndb_init();
// Object representing the cluster
Ndb_cluster_connection* cluster_connection = new Ndb_cluster_connection(connection_string);
if (cluster_connection->connect(3, 2, 1)) {
std::cout << "Cluster management server not ready,error:" << cluster_connection->get_latest_error_msg() << "\n";
exit(-1);
}
if (cluster_connection->wait_until_ready(10, 0) < 0) {
std::cout << "Cluster not ready within 10 secs, error:" << cluster_connection->get_latest_error_msg() << std::endl;
}
return cluster_connection;
}
extern "C" DLLEXPORT char** CALLCV read(Ndb_cluster_connection* cluster_connection) {
Ndb *ndb_obj = new Ndb(cluster_connection, db_name);
if (ndb_obj->init()) {
std::cout << "Error while initializing Ndb " << std::endl;
}
const NdbDictionary::Dictionary* dict = ndb_obj->getDictionary();
const NdbDictionary::Table *table = dict->getTable("table_name"); <-- THIS IS THE LINE THAT THROWS ACCESS VIOLATION EXCEPTION
..
}
在 MySQL 集群邮件列表的帮助下,我找到了解决此问题的方法。
如果想在调用ndb_init()
获得Ndb_cluster_connection
的不同线程中通过NDBAPI读取数据,需要调用mysql_thread_init()
在另一个线程的开头。但是,这不是预期的行为,也没有记录,所以我提交了 a bug.
我遇到了一个有趣的问题,该问题仅在 Windows 上发生,而在 Linux/Mono 上一切正常。我围绕 MySQL Cluster NDB API 库构建了一个 C++ 包装器,我通过 P/Invoke 从 C# 代码调用它。到目前为止我有三种方法:
init()
初始化环境和 returns 句柄read()
使用handle 从数据库中读取数据
release()
释放资源
init()
在每个设置中都可以正常工作。但是,read()
抛出 AccessViolationException
,但仅在 Windows 上并且仅在一个线程中调用 init()
而在另一个线程中调用 read()
的情况下。如果我们在一个线程中做所有事情,那就行得通了!任何有关可能原因的想法将不胜感激。
更新 12/4/2016 - 在一个简单的 C++ 测试程序中注意到了相同的行为,其中 init()
在一个线程中被调用并且 read()
在另一个。因此,错误与 C#/C++ 互操作无关,它是 NDB API 库中的东西,可以下载 here (lib
文件夹,mysqlclient.lib
和 ndbclient_static.lib
).
C#代码:
private const string ndbapi = "ndb";
[DllImport(ndbapi)]
public extern static IntPtr init(IntPtr conn_string);
[DllImport(ndbapi)]
public extern static void read(IntPtr ndb_cluster_conn);
[DllImport(ndbapi)]
public extern static void release(IntPtr ndb_cluster_conn, IntPtr char_arr_ptr);
private static IntPtr handle;
private static void InitNdb() {
unsafe {
fixed (byte* conn_buf = Encoding.UTF8.GetBytes("node1:1186")) {
handle = ndb_api_init(new IntPtr(conn_buf));
}
}
}
static void Main(string[] args) {
Thread t = new Thread(InitNdb);// IF I CALL InitNDB IN THE SAME THREAD, EVERYTHING WORKS
t.Start();
.. //waiting for Thread t to complete...
IntPtr bytes_read = read(handle);
...
}
C++ 代码:(基于 official documentation 中的示例):
#if defined(WIN32)
#define DLLEXPORT __declspec(dllexport)
#define CALLCV __stdcall
#else
#define DLLEXPORT __attribute__((visibility("default")))
#define CALLCV __attribute__((cdecl))
#endif
..
extern "C" DLLEXPORT Ndb_cluster_connection* CALLCV init(char* connection_string) {
ndb_init();
// Object representing the cluster
Ndb_cluster_connection* cluster_connection = new Ndb_cluster_connection(connection_string);
if (cluster_connection->connect(3, 2, 1)) {
std::cout << "Cluster management server not ready,error:" << cluster_connection->get_latest_error_msg() << "\n";
exit(-1);
}
if (cluster_connection->wait_until_ready(10, 0) < 0) {
std::cout << "Cluster not ready within 10 secs, error:" << cluster_connection->get_latest_error_msg() << std::endl;
}
return cluster_connection;
}
extern "C" DLLEXPORT char** CALLCV read(Ndb_cluster_connection* cluster_connection) {
Ndb *ndb_obj = new Ndb(cluster_connection, db_name);
if (ndb_obj->init()) {
std::cout << "Error while initializing Ndb " << std::endl;
}
const NdbDictionary::Dictionary* dict = ndb_obj->getDictionary();
const NdbDictionary::Table *table = dict->getTable("table_name"); <-- THIS IS THE LINE THAT THROWS ACCESS VIOLATION EXCEPTION
..
}
在 MySQL 集群邮件列表的帮助下,我找到了解决此问题的方法。
如果想在调用ndb_init()
获得Ndb_cluster_connection
的不同线程中通过NDBAPI读取数据,需要调用mysql_thread_init()
在另一个线程的开头。但是,这不是预期的行为,也没有记录,所以我提交了 a bug.