๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“– C๋ž‘ C++

Error ํ•ด๊ฒฐ: boost::shared_ptr<boost::asio::io_context>::operator*

boost thread๋ฅผ ํ™œ์šฉํ•˜์—ฌ io_context_pool์„ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋˜ ์ค‘.. core๊ฐ€ ๋‚ฌ๋‹ค

์ด์   ๋†€๋ž์ง€๋„ ์•Š์€..

์ฐธ๊ณ ํ•œ ThreadPool ์˜ˆ์‹œ๋Š” ์•„๋ž˜์— ์ฒจ๋ถ€ ๋งํฌ๋กœ ์˜ฌ๋ ค๋‘๊ฒ ๋‹ค

 

 

CMD> ./process
...
/usr/include/boost/smart_ptr/shared_ptr.hpp:728: typename boost::detail::sp_derr<T>::operator*() const [with T = boost::asio::io_context; typename boost::deta_context&]: Assertion `px != 0' failed.
Aborted (core dumped)

 

๊ทธ๋Ÿฌ๋‹ค ๋น„์Šทํ•œ ์—๋Ÿฌ๋ฅผ ๊ฐ€์ง„ stack overflow ๊ธ€์„ ๋ณด์•˜๋Š”๋ฐ ๋‚ด์šฉ์€ ์ด๋Ÿฌํ–ˆ๋‹ค

 

์ผ๋‹จ ์›์ธ์€, boost ์—์„œ io_context๋ฅผ ์–ป์–ด์˜ค๋Š” ํ•จ์ˆ˜์ธ get_executor() ์‚ฌ์šฉ์‹œ์—

์ž„์‹œ ๊ฐ์ฒด๋กœ ์–ป์–ด์˜ค๋Š” ๊ฒฝ์šฐ์— core๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์—ˆ๋‹ค

 

auto ui_desktop  = std::make_shared<desktop>(ui_context.get_executor());

 

 

get_executor()๋Š” ์•ˆ์—๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋ƒ๋ฉด

 

/* io_context::get_executor */
executor_type get_executor();

 

๊ทธ๋ƒฅ ์š”๋ ‡๋‹ค

 

 

main ํ•จ์ˆ˜์—์„œ ์ž„์‹œ๊ฐ์ฒด excutor์— get_executor()์˜ ๊ฐ’์„ ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด

main ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋ฉด? ์ด ๊ฐ’์€ ์œ ํšจํ•˜์ง€ ์•Š๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค...

๊ทธ๋Ÿฌ๊ณ  ์ด ๊ฐ’์— ๋‹ค์‹œ ์ ‘๊ทผํ•˜๋ฉด ์ž˜๋ชป๋œ ๋ฉ”๋ชจ๋ฆฌ ์ ‘๊ทผ์„ ํ•ด์„œ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค

๋ผ๋Š” ๋‚ด์šฉใ…‹

 

๋งŒ์•ฝ ์–ด์ฉŒ๋‹ค ์ž˜ ๋๋‹ค ํ•˜๋”๋ผ๋„ ์›๋ž˜ ๋ฉ”๋ชจ๋ฆฌ๋ผ๋Š”๊ฒŒ ๊ทธ ํฌ๊ธฐ๋งŒํผ ๊ณต๊ฐ„์ด ์žกํžˆ๋ฉด

๋‹ค์‹œ ๊ทธ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— ์ ‘๊ทผ ์•ˆํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์—†์–ด ๋ณด์ผ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ

๋ฐฉ์‹ฌํ•˜์ง€๋ง์ž

 

๋‹ต๋ณ€์„ ๋‹ฌ์•„์ค€ ๋ถ„์€ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ฌ์•˜๋‹ค

 

ui_desktop ๊ฐœ์ฒด๊ฐ€ ๋ณด์œ ํ•œ ์ฐธ์กฐ๊ฐ€ ์œ ํšจํ•˜๋„๋ก

get_executor์—์„œ ๋ฐ˜ํ™˜๋œ ์‹คํ–‰์ž๊ฐ€ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•ด๋ผ.

์˜ˆ๋ฅผ ๋“ค์–ด, get_executor์˜ ๊ฒฐ๊ณผ๋ฅผ ์ฃผ ๋ณ€์ˆ˜์˜ ๋ณ€์ˆ˜์— ํ• ๋‹น์„ ํ•ด๋ผ.

 

auto executor{ui_context.get_executor()};
auto ui_desktop = std::make_shared<desktop>(executor);

 

์ด๋ ‡๊ฒŒ ใ…‹

 

์ฐธ๊ณ ) object { } ์‚ฌ์šฉ

C++ 11์— ์ถ”๊ฐ€๋œ ์ดˆ๊ธฐํ™” ๋ฌธ๋ฒ•์ธ๋ฐ, ์œ ๋‹ˆํผ ์ดˆ๊ธฐํ™”๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค

int temp(0);์€ ์•ˆ๋˜์ง€๋งŒ int temp{0}์€ ๋œ๋‹ค ใ…‹

 

 


์œ„์˜ ๋ฐฉ๋ฒ•์€ boost io_context๋ฅผ ์ข€๋” ์ž˜ ์“ฐ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ˆ ์ฐธ๊ณ  ๋ฐ”๋ผ๊ณ ,

boost ์˜ˆ์‹œ์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ์„ค๋ช…์„ ๋ง๋ถ™์ด๊ณ  ๋๋‚ด๊ฒ ๋‹ค

 

io_context_pool::io_context_pool(std::size_t pool_size)
  : next_io_context_(0)
{
  if (pool_size == 0)
    throw std::runtime_error("io_context_pool size is 0");

  // Give all the io_contexts work to do so that their run() functions will not
  // exit until they are explicitly stopped.
  for (std::size_t i = 0; i < pool_size; ++i)
  {
    io_context_ptr io_context(new boost::asio::io_context);
    io_contexts_.push_back(io_context);
    work_.push_back(boost::asio::require(io_context->get_executor(),
          boost::asio::execution::outstanding_work.tracked));
  }
}

 

์•„๋ž˜ ์ฒจ๋ถ€์— ๋งํฌ๋ฅผ ๋‘” io_context_pool class์˜ ์ƒ์„ฑ์ž์ธ๋ฐ,

์–˜๋Š” work_๋ผ๋Š” net::io_context::work๋ฅผ ๋‹ด์•„๋‘” vector์—

์œ„์—์„œ ์–ธ๊ธ‰ํ•œ get_executor()๋ฅผ ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด๋‘”๋‹ค

 

boost::asio::io_context& io_context_pool::get_io_context()
{
  // Use a round-robin scheme to choose the next io_context to use.
  boost::asio::io_context& io_context = *io_contexts_[next_io_context_];
  ++next_io_context_;
  if (next_io_context_ == io_contexts_.size())
    next_io_context_ = 0;
  return io_context;
}

 

๊ทธ๋ฆฌ๊ณ  io_context_pool class์— ๋‚ด๋ถ€ ํ•จ์ˆ˜๋กœ get_io_context๋ฅผ ๋‘์–ด

๋ฏธ๋ฆฌ ๊ฐ’์„ ์ €์žฅํ•œ io_context ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋„๋ก ๊ตฌํ˜„ ๋˜์–ด ์žˆ๋‹ค

 

์‹ค์ œ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค

 

void server::start_accept()
{
  new_connection_.reset(new connection(
        io_context_pool_.get_io_context(), request_handler_));
  acceptor_.async_accept(new_connection_->socket(),
      boost::bind(&server::handle_accept, this,
        boost::asio::placeholders::error));
}

 

 


* ์ฐธ๊ณ ํ•œ Stack overflow ๊ธ€ *

https://stackoverflow.com/questions/65534284/boost-asio-segfault-in-release-mode

 

Boost ASIO segfault in release mode

I have made this small illustrative code that exhibits the same issues the program I'm writing does: namely, it works fine in debug mode, segfaults in release. The problem seems to be that the ui_c...

stackoverflow.com

 

* boost thread pool example *

https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/example/cpp03/http/server2/io_context_pool.cpp

 

doc/html/boost_asio/example/cpp03/http/server2/io_context_pool.cpp - 1.78.0

 

www.boost.org