我正在使用一组结构来存储来自不同客户端的不同二进制数据。在我调试的时候,我可以成功进行一些迭代(在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}
答案 0 :(得分:5)
您在代码中复制了错误的金额:
memcpy(buffers.data_ + buffers.end_index, data, buffers.end_index+nbytes);
那应该只是
memcpy(buffers.data_ + buffers.end_index, data, nbytes);
答案 1 :(得分:0)
在@JSF的巨大帮助下
void buffering_mem(char* chunk,int size_chunk, int close_file, int client, int total_size){
//old place of invocation
if (close_file == 3) {
fp0 = fopen("buffer_inteiro", "wb");
fwrite(buffer_audio[client].data_, 1,total_size,fp0);
fflush(fp0); return;
}
int check_bytes = writing_bufer(buffer_audio[client], chunk, size_chunk);
}