【问题标题】:declaration requires an exit-time destructor [-Werror,-Wexit-time-destructors]声明需要退出时析构函数 [-Werror,-Wexit-time-destructors]
【发布时间】:2021-03-18 23:15:00
【问题描述】:

我正在尝试为 android 编译 WebRtc 库,但我遇到了这个令人震惊的错误。请如果有人可以在这件事上帮助我。我有这些实现:

stream_buffer.h

#ifndef AUDIO_STREAM_BUFFER_H_
#define AUDIO_STREAM_BUFFER_H_
​
#include <array>
#include <cstdint>
#include <iostream>
#include <list>
#include <map>
#include <utility>
​
// #include "api/scoped_refptr.h"
#include "rtc_base/constructor_magic.h"
#include "rtc_base/ref_count.h"
#include "rtc_base/string_encode.h"
#include "rtc_base/system/rtc_export.h"
#include "rtc_base/thread_checker.h"
​
namespace rtc {
​
class RTC_EXPORT stream_buffer {
 
 public:
  
  stream_buffer() = delete;
  ~stream_buffer();
​
  static std::map<unsigned char, std::list<unsigned char>> buffer;
  mutable int ref_count_ RTC_GUARDED_BY(buffer) = {};
​
  // typedef unsigned char uint_8;
  typedef unsigned char uint8_t;
​
  static void push(uint8_t ssrc, std::list<uint8_t> value);
​
  static void pop();
​
  static std::list<unsigned char> get_payload(uint8_t ssrc);
​
  static void get_buffer_stats();
​
 private:
​
  RTC_DISALLOW_COPY_AND_ASSIGN(stream_buffer);
};
​
}  // namespace rtc
​
#endif  // AUDIO_STREAM_BUFFER_H_

stream_buffer.cc

#include "stream_buffer.h"
​
namespace rtc {
​
​
 std::map<unsigned char, std::list<unsigned char>> rtc::stream_buffer::buffer = {};
​
void stream_buffer::push(uint8_t ssrc, std::list<uint8_t> buf) {
  std::list<unsigned char> temp = std::move(buf);
​
  if (buffer.find(ssrc) == buffer.end()) {
    buffer.insert(std::pair<uint8_t, std::list<uint8_t>>(ssrc, temp));
  } else {
    buffer.find(ssrc)->second.push_back(temp.front());
    std::cout << "SSRC: " << ssrc
              << " bufferSize: " << buffer.find(ssrc)->second.size() << '\n';
  }
}
​
void stream_buffer::pop() {
  for (auto it = buffer.begin(); it != buffer.end(); ++it) {
    if (!buffer.find(it->first)->second.empty()) {
      std::cout << "key: " << int(it->first) << " pop value: ";
      for (auto v : buffer[int(it->first)]) {
        std::cout << int(v) << ' ';
      }
      std::cout << "\n";
      buffer.find(it->first)->second.pop_front();
    } else {
      std::cout << "Empty buffer for " << it->first << '\n';
    }
  }
}
​
std::list<unsigned char> stream_buffer::get_payload(
    stream_buffer::uint8_t ssrc) {
  if (buffer.find(ssrc) != buffer.end()) {
    return buffer[ssrc];
  } else {
    std::cout << "\nstream_buffer.cpp -> Invalid SSRC.\n";
    return static_cast<std::list<unsigned char>>(0);
  }
}
​
void stream_buffer::get_buffer_stats() {
  std::cout << "\nstream_buffer.cpp -> stream_buffer stats:\n";
  for (auto it = buffer.begin(); it != buffer.end(); ++it) {
    if (buffer.find(it->first) != buffer.end()) {
      std::cout << "SSRC: " << int(it->first)
                << " bufferSize: " << buffer.find(it->first)->second.size()
                << '\n';
    }
  }
}
}

基本上我已经在上面的一个类中声明了一个静态映射,名称为stream_buffer::buffer。 当我尝试编译库时,出现以下错误。

stream_buffer.cc 文件的这一行。

std::map<unsigned char, std::list<unsigned char>> rtc::stream_buffer::buffer = {};

我已经访问过多个堆栈溢出问题,但没有发现任何有用的东西。

  1. How to deal with “exit-time destructor” warning in clang?

我非常感谢您在这两个问题上指出正确的方向。

【问题讨论】:

  • 你试过用这个std::map&lt;unsigned char, std::list&lt;unsigned char&gt;&gt; rtc::stream_buffer::buffer{{1, {1, 2, 3, 4}}};? May be it is due to initialization of std::map.
  • @MuhammadUsman 是的,先生,我试过了。不是地图初始化的问题。
  • @DanielLangr,先生,我已经多次访问过这个问题,但它没有帮助我。你能建议我一些其他的解决方案吗?从上周开始,我已经在 Google 上进行了几次搜索。
  • 你能故意泄露地图而不是让它在程序终止时被破坏吗?你能做到地图在首次访问时构建(使用实例 getter 辅助函数),而不是在调用 main 之前的某个时间构建吗?

标签: c++ gcc clang webrtc


【解决方案1】:

您可以动态创建地图。简化的东西:

class X
{
   static std::map<int, int>* ptr_;        

public:
   static std::map<int, int>& buffer()
   {
      return *ptr_;
   }
 
   static void finalize()
   {
      delete ptr_;
   }
};

std::map<int, int>* X::ptr = new std::map<int, int>{};        

现在,由于静态变量是一个普通的指针,构造函数和析构函数都没有问题。

请注意,您需要手动删除地图,此处通过在程序末尾的某处调用X::finalize()

【讨论】:

    猜你喜欢
    • 2016-01-20
    • 2014-08-01
    • 2011-12-13
    • 1970-01-01
    • 2014-02-01
    • 2021-11-20
    • 2015-01-02
    • 1970-01-01
    • 2013-05-22
    相关资源
    最近更新 更多