簡體   English   中英

shared_ptr與new運算符:使用哪個運算符

[英]shared_ptr vs. new operator: which one to use

在下面的函數中,我利用了http_clienthttps://github.com/Microsoft/cpprestsdk )的http_client向網絡攝像機發出http請求。 下面的函數可能是在發出特定請求時由lcm庫( http://lcm-proj.github.io/ )調用的回調。

我在第11行遇到問題。我以前使用過new運算符:

auto init_session_response = new init_session_response_t;

創建指針並在退出函數之前手動將其刪除。 但是在第49行的pplx任務繼續中嘗試修改init_session_response對象時遇到訪問沖突異常。

init_session_response->status_code =
              ptz_camera::status_codes_t::OK;

當我開始使用std :: shared_ptr時,這個問題就消失了。 有人可以向我解釋為什么使用shared_ptr解決了這個問題嗎? 還應該使用std :: shared_ptr創建http_client *嗎?

1 void lcm_handler::on_init_session_req(const lcm::ReceiveBuffer* rbuff,
2   const std::string& channel,
3   const ptz_camera::init_session_request_t* req)
4 {
5     std::cout << "Received init session req on channel: " << channel << 
6       "; Camera: " << req->ip_address << std::endl;
7 
8   auto ip_address = req->ip_address;
9 
10    // Note use of std::shared_ptr
11  auto init_session_response = make_shared<ptz_camera::init_session_response_t>();
12  
13  auto key_position = this->ip_client_map.find(ip_address);
14  if (key_position == ip_client_map.end())
15  {
16      std::cout << "Creating a new client for the ip: "
17          << req->ip_address << endl;
18
19      wstring username = this->convert_to_wstring(req->username);
20      wstring password = this->convert_to_wstring(req->password);
21
22      wstring main_uri = L"http://" + convert_to_wstring(ip_address);
23      auto config = http_client_config();
24      auto login = credentials(username, password);
25      config.set_credentials(login);
26      config.set_timeout(std::chrono::milliseconds(500));
27
28      http_client* client = new http_client(main_uri, config);
29      std::cout << "Client created...\n";
30
31      uri_builder uri = uri_builder(U("/") + uri_constants::stw_cgi).
32          append_path(uri_constants::attributes_cgi).append_path(uri_constants::attributes);
33
34      auto request = uri.to_string();
35  
36      client->request(methods::GET, request)
37          .then([this, ip_address, client, init_session_response]
38              (pplx::task<http_response> request_task) -> pplx::task<wstring>
39      {
40          try 
41          {
42              auto response = request_task.get();
43              if (response.status_code() == status_codes::OK)
44              {
45                  std::cout << "Saving client...";
46                  this->ip_client_map[ip_address] = client;
47                  std::cout << "success.\n";
48
49                  init_session_response->status_code =
50                  ptz_camera::status_codes_t::OK;
51              }
52
53              else
54              {
55                  cout << "GET request to client failed! HTTP Error: "
56                      << response.status_code() << std::endl;
57
58                  init_session_response->status_code =
59                      ptz_camera::status_codes_t::ERR;
60              }
61
62              return response.extract_string();
63          }
64              
65          catch (const exception& e)
66          {
67              cout << "Caught exception: " << e.what() << endl;
68              return create_task([e, this]() -> wstring
69              {
70                  return convert_to_wstring(e.what());
71              });
72          }               
73
74      })
75          .then([init_session_response, this](wstring response)
76      {
77          string n = this->convert_to_string(response);
78          init_session_response->response_message = n;
79      });
80  }
81
82
83  else
84  {
85      string message = "Client for ip: " + req->ip_address + " already exists\n";
86      cout << message << endl;
87      init_session_response->response_message = message;
88      init_session_response->status_code = ptz_camera::status_codes_t::OK;
89  }   
90
91  this->lcm->publish(ptz_camera_channels::init_session_res_channel,
92      init_session_response.get());   
93}

當您收到違規訪問錯誤時,您必須在代碼中的某個位置刪除了指針(例如,在then lambda中),但是您沒有使用原始指針發布代碼,因此我無法說出哪一行。

通過使用std::shared_ptr ,當它傳遞給lambda時,它會被值捕獲,因此它增加了use_count並確保init_session_response有效且在labmda中不被破壞,從而解決了該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM