GithubHelp home page GithubHelp logo

Comments (6)

eidheim avatar eidheim commented on July 19, 2024

Might be better if you create a pull request, since you got the code already to test it and see if you get rid of the warning:)

from simple-websocket-server.

eidheim avatar eidheim commented on July 19, 2024

Seems a bit strange though to be needing to create a destructor for this, since destructors should generally not be created manually if they are not needed.

from simple-websocket-server.

pp23 avatar pp23 commented on July 19, 2024

I will create a pull request later on. The warning (and the resulting segfault during execution) occurs in a test code of a bigger project. But I can easily create a simple separated server/client code for an example.

I have already searched a bit on the web and found this useful answer on stackoverflow: http://stackoverflow.com/a/827203/1926463

from simple-websocket-server.

eidheim avatar eidheim commented on July 19, 2024

From http://programmers.stackexchange.com/questions/284561/when-not-to-use-virtual-destructors: "The reason is subclassing + use of polymorphism. A virtual destructor is required only if a dynamic resolution is needed, that is a reference/pointer/whatever to the master class may actually refer to an instance of a subclass". If your pointer is always of type SocketClient for instance, the virtual destructor is not needed if I understand it correctly, the problem is if you are in need of dynamic casting (for instance your pointer is of type SocketClientBase and you want to be able cast it to either SocketClient or SocketClient). Or am I wrong?

from simple-websocket-server.

pp23 avatar pp23 commented on July 19, 2024

The code below is crashing with the following stack trace:

0   malloc_consolidate      4157    0x7ffff67b88f3  
1   _int_free       4057    0x7ffff67b956d  
2   __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream, std::allocator<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream>, (__gnu_cxx::_Lock_policy)2>>::deallocate    new_allocator.h 110 0x42b08c    
3   std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream, std::allocator<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream>, (__gnu_cxx::_Lock_policy)2>>>::deallocate   alloc_traits.h  377 0x4295b1    
4   std::_Sp_counted_ptr_inplace<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream, std::allocator<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream>, (__gnu_cxx::_Lock_policy)2>::_M_destroy  shared_ptr_base.h   417 0x43242a    
5   std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release   shared_ptr_base.h   161 0x41a022    
6   std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count    shared_ptr_base.h   546 0x416c19    
7   std::__shared_ptr<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr shared_ptr_base.h   781 0x41459c    
8   std::shared_ptr<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream>::~shared_ptr shared_ptr.h    93  0x4145b6    
9   SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::send(std::shared_ptr<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream>, std::function<void (boost::system::error_code const&)> const&, unsigned char)::{lambda()#1}::~function()    client_ws.hpp   170 0x418dda    
10  boost::asio::detail::completion_handler<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::send(std::shared_ptr<SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::SendStream>, std::function<void (boost::system::error_code const&)> const&, unsigned char)::{lambda()#1}>::do_complete(boost::asio::detail::task_io_service*, boost::asio::detail::task_io_service_operation*, boost::system::error_code const&, unsigned long)  completion_handler.hpp  59  0x427775    
11  boost::asio::detail::task_io_service_operation::destroy task_io_service_operation.hpp   43  0x40dbba    
12  boost::asio::detail::op_queue_access::destroy<boost::asio::detail::task_io_service_operation>   op_queue.hpp    47  0x41985e    
13  boost::asio::detail::op_queue<boost::asio::detail::task_io_service_operation>::~op_queue    op_queue.hpp    81  0x4150ce    
14  boost::asio::detail::strand_service::shutdown_service   strand_service.ipp  66  0x41353b    
15  boost::asio::detail::service_registry::~service_registry    service_registry.ipp    37  0x40d783    
16  boost::asio::io_service::~io_service    io_service.ipp  53  0x410551    
17  SimpleWeb::SocketClientBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::~SocketClientBase    client_ws.hpp   19  0x413be3    
18  SimpleWeb::SocketClient<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp>>>::~SocketClient    client_ws.hpp   443 0x41457d        
typedef SimpleWeb::SocketClient<SimpleWeb::WS> WsClient;

static const size_t clientCount = 250;

BOOST_AUTO_TEST_CASE(OpenTest) {
    std::atomic<int> openedClients(0);
    WsClient *client[clientCount];
    std::thread t[clientCount];
    for(size_t i = 0; i < clientCount; ++i) {
        client[i] = new WsClient("localhost:8080/a");
        client[i]->onopen = [&client,i,&openedClients]() {
            client[i]->send_close(0);
            client[i]->stop();
            ++openedClients;
        };
    }
    for(size_t i = 0; i < clientCount; ++i) {
        WsClient* c = client[i];
        BOOST_REQUIRE_MESSAGE(c != nullptr, "Client is null: " + std::to_string(i));
        if(c != nullptr) {
            t[i] = std::thread([c](){ try {
                    c->start();
                }catch(std::invalid_argument e){
                    std::cerr << "Exception: " << e.what() << std::endl;
                }
            });
        }
    }

    for(size_t i = 0; i < clientCount; ++i) {
        t[i].join();
    }

    for(size_t i = 0; i < clientCount; ++i) {
        delete client[i];                                    //SegFault
    }

    BOOST_TEST(openedClients == clientCount);
}

from simple-websocket-server.

eidheim avatar eidheim commented on July 19, 2024

I see now that http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/reference/io_service/stop.html is not blocking, so the destructor (#28) should maybe wait until http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/reference/io_service/stopped.html is true instead. I'll have a look at this in a day or two. At least you have a working version in #28.

Edit: disregard this message too, your solution seems like the best one so far.

from simple-websocket-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.