小编use*_*087的帖子

avcodec_encode_video2 时出现分段错误

尝试将 AVFrame 编码为数据包时遇到一些问题。

在阅读整个代码之前,输入的东西正在工作,我对其进行了测试。输出的东西来自这里的一个例子。我认为有问题。但是分段错误发生在接近结束的循环中。

这是我的简化代码:

void nmain() {

  // input stuff
  AVFormatContext *formatCtxIn=0;
  AVInputFormat *formatIn=0;
  AVCodecContext *codecCtxIn=0;
  AVCodec *codecIn;
  AVPacket *pktIn;

  av_register_all();
  avdevice_register_all();
  avcodec_register_all();


  formatIn = av_find_input_format("dshow");

  if(!formatIn)
    return;


  AVDictionary *avoption=0;
  av_dict_set(&avoption, "rtbufsize", "1000000000", NULL);

  if(avformat_open_input(&formatCtxIn, "video=Integrated Camera", formatIn, &avoption)!=0)
    return;

  if(avformat_find_stream_info(formatCtxIn, NULL)<0)
    return;

  codecCtxIn = formatCtxIn->streams[0]->codec;
  codecIn = avcodec_find_decoder(codecCtxIn->codec_id);

  if(avcodec_open2(codecCtxIn, codecIn, NULL)<0)
    return;


  // end input stuff  
//------------------------------------------------------------------------------
  // output stuff

  AVOutputFormat *formatOut=0;
  AVFormatContext *formatCtxOut=0;
  AVStream *streamOut=0;
  AVFrame *frame=0;
  AVCodec *codecOut=0;
  AVPacket *pktOut;

  const char *filename = …
Run Code Online (Sandbox Code Playgroud)

c++ libav

5
推荐指数
1
解决办法
6330
查看次数

mixin继承与可变参数模板的可见性规则

考虑我从一个可变参数模板继承整个参数列表.参数如何继承?

// snippet
template<typename... R>
class foo
    : public R... {
public: 
};
// ....
using foo_inst = foo<bar_1, bar_2>;
Run Code Online (Sandbox Code Playgroud)

我试过了,似乎所有 R的都是公共的继承(不仅仅是第一个).这是定义的行为吗?

我用gcc和msvc试了一下(感谢jaggedSpire也用clang),都有相同的结果.编译器甚至没有提到任何警告.你可以在这里看到一个运行的例子.

c++ inheritance template-mixins variadic-templates c++11

5
推荐指数
1
解决办法
148
查看次数

boost::asio 异步写入需要互斥锁吗?

我试图理解 boost::asio 但有一些问题。在这个例子中如果我错了请纠正我。

  1. 该消息通过对 write() 方法的引用和通过对 do_write() 的值给出。所以我认为这没问题,即使 do_write 是由 io_service.post 完成的,boost::bind 也是按值绑定消息。

  2. 但是为什么write_msgs_队列没有互斥锁,因为 std::deque 可能会四处移动或复制其元素,如果需要并且 io_service::run 有自己的线程,则无法确保数据一致。

  3. 用指针来做不是更好。如果消息太长,则必须始终按值复制它们。但是使用 new 和 delete 它们将在发送之前创建并在发送后删除。然后我会像这样发送

代码

boost::asio::async_write(socket_,
      boost::asio::buffer(*write_msgs_.front().data(),
        write_msgs_.front().length()),
      boost::bind(&chat_client::handle_write, this,
        boost::asio::placeholders::error));
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-asio

4
推荐指数
1
解决办法
3223
查看次数

std :: initializer_list作为构造函数的模板参数

考虑一个继承自std容器的类,该类具有调用容器的底层构造函数的模板构造函数.此模板构造函数适用于简单复制和移动构造函数,但不适用于initializer_list ctor.

template<typename container_T>
class test : public container_T {
public:
  using container_type = container_T;

  test() {} 

  // templated constructor
  template<typename T>
  test(T t)
    : container_T(t) {}

   // without this it won't compile
  test(std::initializer_list<typename container_T::value_type> l)
    : container_T(l) {}
};

int main() {    
  test<std::deque<int>> vdi1;
  test<std::deque<int>> vdi2({1,2,3,4,5,6,7,8,9});

  std::cout << "vdi2 before:" << std::endl;
  for(auto it : vdi2)
    std::cout << it << std::endl;

  test<std::deque<int>> vdi3(std::move(vdi2));

  std::cout << "vdi2 before:" << std::endl;
  for(auto it : vdi2)
    std::cout << it << std::endl;

  std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ constructor initializer-list c++11 type-deduction

4
推荐指数
1
解决办法
1193
查看次数

std :: unique_lock <std :: mutex>禁止dll卸载

卸载dll时遇到问题.它就像这个但是不同.我正在使用LoadLibraryA加载一个DLL 然后调用一个函数并用FreeLibrary关闭dll .但是,dll未卸载但FreeLibrary返回成功.减少代码:

void foo() {
    std::unique_lock<std::mutex> lock(mtx_);
}
Run Code Online (Sandbox Code Playgroud)

在调试代码并查看Process Explorer时, unique_lock会创建第二个线程,但为什么呢?只要应用程序运行,此线程也会运行.没有别的; dll没有其他句柄,没有其他功能.dll仍然在程序中加载.如果我删除上面的行,一切都很好.dll卸载正常,没有额外的线程.所以我的问题是,如何避免这种行为以及为什么unique_lock创建一个线程?

互斥体用于多线程,但在测试时,只有一个线程,加载dll调用foo,并卸载dll.

编辑:

我不知道这是mutex/unique_lock的visual studio实现中的错误,但我通过使用boost的mutex/unique_lock解决了这个问题.

c++ windows dll

4
推荐指数
1
解决办法
554
查看次数

我怎么能不将函数(std :: bind)包装到命名空间中?

我想将绑定类模板包装到一个单独的命名空间:

namespace my_space {
template<typename... R> using bind = std::bind<R...>;
}
Run Code Online (Sandbox Code Playgroud)

并得到一个错误:

error: 'bind<R ...>' in namespace 'std' does not name a type.
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?这里可以找到一个小例子.

c++ bind wrapper

4
推荐指数
1
解决办法
162
查看次数

使用递归函数查找最小值

无论输入是什么,结果始终为0.为什么?

#include <stdio.h>
#include <conio.h>

int rekursiv( int v[], int i, int n, int *min );

int main( void )
{
    int v[ 100 ];
    int n, i, min;

    printf( "Shkruanni n: " );
    scanf( "%d", &n );
    printf( "Shkruani elementet e vektorit.\n" );
    for( i = 0; i < n; i++ ){
         scanf( "%d", &v[ i ] );

         }//end for
    min = v[ 0 ];
    i = 1;
    printf( "Minimumi eshte %d.", rekursiv( v, i, n, &min ) ); …
Run Code Online (Sandbox Code Playgroud)

c recursion minimum

4
推荐指数
1
解决办法
161
查看次数

是否可以重新定位 .gitignore 文件本身?

我在一个文件夹中有两个 git 存储库。我能够重新.git定位目录并使用--git-dir=/path/to/.git和来处理两个存储库--work-tree=/path/to/code。但是,我找不到任何重新定位.gitignore文件的选项,该文件当前位于存储库的根路径中,并且它们都被考虑。我也考虑过使用-c <.gitignoreOptionWhichIDontKnow>="/path/to/.gitignore"但找不到任何选项来重新定位我的 .gitignore 文件。

是否可以重新定位 .gitignore 文件?

git gitignore

4
推荐指数
1
解决办法
2120
查看次数

如何将对int8_t的引用转换为对uint8_t的引用?

我尝试将int8_t的引用转换为uint8_t的引用.

我有以下代码:

inline mtype& operator&(mtype& mt, uint8_t& va) {
  // do something
  // ...

  return mt;
}

inline mtype& operator&(mtype& mt, int8_t& va) {
  // do the same but signed
  // ...

  return mt;
}
Run Code Online (Sandbox Code Playgroud)

因为这两种重载做同样的,我要(或更好的DRM),所以我想打电话与第一运营商casted va.但是我该怎么做?这不行.

inline mtype& operator&(mtype& mt, int8_t& va) {
  return mt& static_cast<uint8_t>(va); //  error: no match for 'operator&' in 'mt & (uint8_t)va'
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

c++ casting reference

3
推荐指数
1
解决办法
575
查看次数

为什么const数组无法从constexpr函数访问?

我有一个名为access的constexpr函数,我想从数组中访问一个元素:

char const*const foo="foo";
char const*const bar[10]={"bar"};

constexpr int access(char const* c) { return (foo == c); }     // this is working
constexpr int access(char const* c) { return (bar[0] == c); }  // this isn't
int access(char const* c) { return (bar[0] == c); }            // this is also working
Run Code Online (Sandbox Code Playgroud)

我收到错误:

error: the value of 'al' is not usable in a constant expression
Run Code Online (Sandbox Code Playgroud)

为什么我不能从访问中访问其中一个元素?或者更好我该怎么做,如果有可能的话?

c++ constexpr c++11

3
推荐指数
1
解决办法
605
查看次数