src: establish context on PCSCLite constructor
- Remove SCardEstablishContext from get_card_readers. - Initialize m_status_thread to NULL so calling PCSCLite destructor does not crash in case the thread has not been launched. For example when SCardEstablish context fails - Thanks to Ludovic Rousseau for pointing that out @ https://github.com/santigimeno/node-pcsclite/pull/15#issuecomment-56222251.
This commit is contained in:
@@ -19,8 +19,17 @@ void PCSCLite::init(Handle<Object> target) {
|
||||
target->Set(NanNew("PCSCLite"), tpl->GetFunction());
|
||||
}
|
||||
|
||||
PCSCLite::PCSCLite(): m_card_context(0) {
|
||||
PCSCLite::PCSCLite(): m_card_context(NULL),
|
||||
m_status_thread(NULL) {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
|
||||
LONG result = SCardEstablishContext(SCARD_SCOPE_SYSTEM,
|
||||
NULL,
|
||||
NULL,
|
||||
&m_card_context);
|
||||
if (result != SCARD_S_SUCCESS) {
|
||||
NanThrowError(pcsc_stringify_error(result));
|
||||
}
|
||||
}
|
||||
|
||||
PCSCLite::~PCSCLite() {
|
||||
@@ -29,7 +38,9 @@ PCSCLite::~PCSCLite() {
|
||||
}
|
||||
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
if (m_status_thread) {
|
||||
pthread_cancel(m_status_thread);
|
||||
}
|
||||
}
|
||||
|
||||
NAN_METHOD(PCSCLite::New) {
|
||||
@@ -156,14 +167,6 @@ LONG PCSCLite::get_card_readers(PCSCLite* pcsclite, AsyncResult* async_result) {
|
||||
async_result->readers_name = NULL;
|
||||
async_result->readers_name_length = 0;
|
||||
|
||||
if (!pcsclite->m_card_context) {
|
||||
result = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &pcsclite->m_card_context);
|
||||
}
|
||||
|
||||
if (result != SCARD_S_SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Find out ReaderNameLength */
|
||||
DWORD readers_name_length;
|
||||
result = SCardListReaders(pcsclite->m_card_context, NULL, NULL, &readers_name_length);
|
||||
|
||||
Reference in New Issue
Block a user