我正在使用结构数组来存储来自不同客户端的不同二进制数据。在调试时,我可以成功进行几次迭代(在memcpy中)。但是在某些时候,调试会因“未处理的异常”而崩溃。

struct Buffer {
    int size_ = 0;
    int capacity_total = 200000;
    int beg_index = 0
    int end_index = 0;
    char data_[200000];
} buffer_audio[3];

int writing_bufer(Buffer& buffers, const char *data, int nbytes) {
    if (nbytes == 0) return 0;

    int capacity = buffers.capacity_total;

    if (nbytes <= capacity - buffers.end_index)
    {
        memcpy(buffers.data_ + buffers.end_index, data, nbytes); //crashes here
        buffers.end_index += nbytes;
        if (buffers.end_index == capacity) printf("full");
    }
    else {
        return 0; }

    return buffers.end_index;
}


缓冲区永远不会充满或关闭。
完整的例程:

void buffering(const FunctionCallbackInfo<v8::Value>& args) {
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    int size = args[1]->NumberValue();
    int final_read = args[2]->NumberValue();
    int client_id = args[3]->NumberValue();
    int nbytes = args[4]->NumberValue();

    (...)
    buf = node::Buffer::Data(bufferObj);
    buffering_mem(buf,size, final_read, client_id,nbytes);


    Local<String> devolve = String::NewFromUtf8(isolate, "buffering_com_sucesso");//C++--->JS
    args.GetReturnValue().Set(devolve);
}

void buffering_mem(char* chunk,int size_chunk, int close_file, int client, int total_size){

    int check_bytes = writing_bufer(buffer_audio[client], chunk, size_chunk);
    //other code}

最佳答案

您在代码中复制了错误的金额:

memcpy(buffers.data_ + buffers.end_index, data, buffers.end_index+nbytes);


那应该是

memcpy(buffers.data_ + buffers.end_index, data, nbytes);

08-27 02:16