Skip to content
This repository has been archived by the owner on Apr 6, 2019. It is now read-only.

cpp_redis on Windows, WSACleanup #178

Open
KirSlayk opened this issue May 18, 2018 · 1 comment
Open

cpp_redis on Windows, WSACleanup #178

KirSlayk opened this issue May 18, 2018 · 1 comment

Comments

@KirSlayk
Copy link

Hi, i compiled cpp_redis with tacopie to dll via cmake (cmake -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE -DBUILD_SHARED_LIBS=TRUE -DLOGGING_ENABLED=1 -DMSVC_RUNTIME_LIBRARY_CONFIG=/MTd -G "Visual Studio 15 2017 Win64" -DCMAKE_CXX_STANDARD=11) using visual studio 17 on WINDOWS 10.

The code sample is the following:

#include <cpp_redis/cpp_redis>
#include <iostream>

#ifdef _WIN32
#include <Winsock2.h>
#endif /* _WIN32 */

int
main(void) {
#ifdef _WIN32
    //! Windows netword DLL init
    WORD version = MAKEWORD(2, 2);
    WSADATA data;

    if (WSAStartup(version, &data) != 0) {
        std::cerr << "WSAStartup() failure" << std::endl;
        return -1;
    }
#endif /* _WIN32 */

    //! Enable logging

    cpp_redis::client client;

    client.client_setname("redis");
    client.select(1);
    client.connect("192.168.31.166", 6379, [](const std::string& host, std::size_t port, cpp_redis::client::connect_state status) {
        if (status == cpp_redis::client::connect_state::dropped) {
            std::cout << "client disconnected from " << host << ":" << port << std::endl;
        }
    });

    // same as client.send({ "SET", "hello", "42" }, ...)
    client.set("hello", "42", [](cpp_redis::reply& reply) {
        std::cout << "set hello 42: " << reply << std::endl;
    });

    // same as client.send({ "DECRBY", "hello", 12 }, ...)
    client.decrby("hello", 12, [](cpp_redis::reply& reply) {
        std::cout << "decrby hello 12: " << reply << std::endl;
    });

    // same as client.send({ "GET", "hello" }, ...)
    client.get("hello", [](cpp_redis::reply& reply) {
        std::cout << "get hello: " << reply << std::endl;
    });

    client.sync_commit();
    client.disconnect(true);


#ifdef _WIN32
    WSACleanup();
#endif /* _WIN32 */

    return 0;
}

output error:

f:\dd\vctools\crt\crtw32\stdcpp\thr\mutex.c(51): mutex destroyed while busy

stack trace:

 	cpp_redisD.dll!issue_debug_notification(const wchar_t * const message) Line 28	C++	Symbols loaded.
 	cpp_redisD.dll!__acrt_report_runtime_error(const wchar_t * message) Line 154	C++	Symbols loaded.
 	cpp_redisD.dll!abort() Line 61	C++	Symbols loaded.
>	cpp_redisD.dll!_Thrd_abort(const char * msg) Line 119	C	Non-user code. Symbols loaded.
 	cpp_redisD.dll!_Mtx_destroy_in_situ(_Mtx_internal_imp_t * mtx) Line 52	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!std::_Mutex_base::~_Mutex_base() Line 41	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!std::mutex::~mutex()	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!tacopie::io_service::~io_service() Line 85	C++	Symbols loaded.
 	cpp_redisD.dll!tacopie::io_service::`scalar deleting destructor'(unsigned int)	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!std::_Ref_count_obj<tacopie::io_service>::_Destroy() Line 1860	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!std::_Ref_count_base::_Decref() Line 895	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!std::_Ptr_base<tacopie::io_service>::_Decref() Line 1169	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!std::shared_ptr<tacopie::io_service>::~shared_ptr<tacopie::io_service>() Line 1460	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!tacopie::`dynamic atexit destructor for 'io_service_default_instance''()	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!_execute_onexit_table::__l2::<lambda>() Line 206	C++	Symbols loaded.
 	cpp_redisD.dll!__crt_seh_guarded_call<int>::operator()<void <lambda>(void),int <lambda>(void) & __ptr64,void <lambda>(void) >(__acrt_lock_and_call::__l2::void <lambda>(void) && setup, _execute_onexit_table::__l2::int <lambda>(void) & action, __acrt_lock_and_call::__l2::void <lambda>(void) && cleanup) Line 204	C++	Symbols loaded.
 	cpp_redisD.dll!__acrt_lock_and_call<int <lambda>(void) >(const __acrt_lock_id lock_id, _execute_onexit_table::__l2::int <lambda>(void) && action) Line 940	C++	Symbols loaded.
 	cpp_redisD.dll!_execute_onexit_table(_onexit_table_t * table) Line 231	C++	Symbols loaded.
 	cpp_redisD.dll!common_exit::__l2::<lambda>() Line 226	C++	Symbols loaded.
 	cpp_redisD.dll!__crt_seh_guarded_call<void>::operator()<void <lambda>(void),void <lambda>(void) & __ptr64,void <lambda>(void) >(__acrt_lock_and_call::__l2::void <lambda>(void) && setup, common_exit::__l2::void <lambda>(void) & action, __acrt_lock_and_call::__l2::void <lambda>(void) && cleanup) Line 224	C++	Symbols loaded.
 	cpp_redisD.dll!__acrt_lock_and_call<void <lambda>(void) >(const __acrt_lock_id lock_id, common_exit::__l2::void <lambda>(void) && action) Line 940	C++	Symbols loaded.
 	cpp_redisD.dll!common_exit(const int return_code, const _crt_exit_cleanup_mode cleanup_mode, const _crt_exit_return_mode return_mode) Line 259	C++	Symbols loaded.
 	cpp_redisD.dll!_cexit() Line 314	C++	Symbols loaded.
 	cpp_redisD.dll!__scrt_dllmain_uninitialize_c() Line 407	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!dllmain_crt_process_detach(const bool is_terminating) Line 107	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!dllmain_crt_dispatch(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 138	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!dllmain_dispatch(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 211	C++	Non-user code. Symbols loaded.
 	cpp_redisD.dll!_DllMainCRTStartup(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 253	C++	Non-user code. Symbols loaded.
 	ntdll.dll!LdrpCallInitRoutine()	Unknown	Non-user code. Symbols loaded.
 	ntdll.dll!LdrShutdownProcess()	Unknown	Non-user code. Symbols loaded.
 	ntdll.dll!RtlExitUserProcess()	Unknown	Non-user code. Symbols loaded.
 	kernel32.dll!ExitProcessImplementation�()	Unknown	Non-user code. Symbols loaded.
 	RedisTest.exe!exit_or_terminate_process(const unsigned int return_code) Line 144	C++	Symbols loaded.
 	RedisTest.exe!common_exit(const int return_code, const _crt_exit_cleanup_mode cleanup_mode, const _crt_exit_return_mode return_mode) Line 282	C++	Symbols loaded.
 	RedisTest.exe!exit(int return_code) Line 294	C++	Symbols loaded.
 	RedisTest.exe!__scrt_common_main_seh() Line 292	C++	Non-user code. Symbols loaded.
 	RedisTest.exe!__scrt_common_main() Line 326	C++	Non-user code. Symbols loaded.
 	RedisTest.exe!mainCRTStartup() Line 17	C++	Non-user code. Symbols loaded.
 	kernel32.dll!BaseThreadInitThunk�()	Unknown	Non-user code. Symbols loaded.
 	ntdll.dll!RtlUserThreadStart�()	Unknown	Non-user code. Symbols loaded.

i deleted this code

#ifdef _WIN32
    WSACleanup();
#endif /* _WIN32 */

and this resolved my problem
Question: why WSAStartup and WSACleanup are not implemented inside tacopie? Why the singleton (io_service_default_instance) is not freed during disconnect(true)? Is there any posibility to shutdown tcp client (tacopie) on demand?

@3Bubble
Copy link

3Bubble commented Jun 13, 2018

win 10 , vs 2017:
It compiles successfully.
1, But** "cpp_redis::reply& reply " is empty.** and cannot get "hello" after client.set.
2, connect_state is OK(3).

`#include "stdafx.h"
#include
#include <\cpp_redis\includes\cpp_redis\core\client.hpp>

#ifdef _WIN32
#include <Winsock2.h>
#endif /* _WIN32 */

int main()
{
#ifdef _WIN32
//! Windows netword DLL init
WORD version = MAKEWORD(2, 2);
WSADATA data;

if (WSAStartup(version, &data) != 0) {
	std::cerr << "WSAStartup() failure" << std::endl;
	return -1;
}

#endif /* _WIN32 */

//! Enable logging
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);

cpp_redis::client client;
client.connect("127.0.0.1", 6379, [](const std::string& host, std::size_t port, cpp_redis::client::connect_state status) {
	if (status == cpp_redis::client::connect_state::dropped) {
		std::cout << "client disconnected from " << host << ":" << port << std::endl;
	}
});

// same as client.send({ "SET", "hello", "42" }, ...)
client.set("hello", "42", [](cpp_redis::reply& reply) {
	std::cout << "set hello 42: " << reply << std::endl;
	// if (reply.is_string())
	//   do_something_with_string(reply.as_string())
});

// same as client.send({ "DECRBY", "hello", 12 }, ...)
client.decrby("hello", 12, [](cpp_redis::reply& reply) {
	std::cout << "decrby hello 12: " << reply << std::endl;
	// if (reply.is_integer())
	//   do_something_with_integer(reply.as_integer())
});

// same as client.send({ "GET", "hello" }, ...)
client.get("hello", [](cpp_redis::reply& reply) {
	std::cout << "get hello: " << reply << std::endl;
	// if (reply.is_string())
	//   do_something_with_string(reply.as_string())
});

// commands are pipelined and only sent when client.commit() is called
// client.commit();

// synchronous commit, no timeout
client.sync_commit();

// synchronous commit, timeout
// client.sync_commit(std::chrono::milliseconds(100));

#ifdef _WIN32
WSACleanup();
#endif /* _WIN32 */

return 0;

}`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants