我面临一个奇怪的 OpenSSL 问题:当我的应用程序作为 win32 服务(本地服务/网络服务或系统服务)运行时,似乎 SSL_CTX_new() 函数崩溃。
我的代码如下所示:
int main() {
SSL_library_init();
const int nLocks = CRYPTO_num_locks();
ossl_mtx_pool = new mutexT* [nLocks]; // simple wrapper class over mutexes
for(int i = 0; i < nLocks; ++i)
ossl_mtx_pool[i] = new mutexT;
CRYPTO_THREADID_set_callback(ossl_threadid_function); // returns win32 thread id
CRYPTO_set_locking_callback(ossl_locking_fun); // simple function that calls mutexT::lock/unlock)
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ERR_load_BIO_strings();
// some other app initialization here...
// run win32 service thread OR just a seaparate thread
}
// called later on in the context of a different thread
int myclient()
{
myCtx *ctx = new connContextT;
const SSL_METHOD *mm = SSLv23_client_method();
if(mm == 0) {
// print some error whcih doesn't show up
}
printf("I'm Here!\n"); //< this print shows up
ctx->sslCtx = SSL_CTX_new(mm);
// crash >HERE< before being able to print anything else
// some code to connect to server
}
真正奇怪的是,如果我将这个应用程序作为普通的 win32 应用程序运行(即不调用 win32 服务函数但仍在单独的线程中运行 myclient()),一切都会完美无缺。
我在win7上使用mingw32 gcc 4.7.1进行编译,并且静态链接openssl(无DLL)。
任何试图理解问题的帮助将不胜感激。谢谢!