0

I am using an array of structs to store different binary data, from different clients. While I am debugging, I can do a few iterations with success (in memcpy). But at some point the debug crashes with "unhandled exception".

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;
}

The buffer never is full or close of that. The full routine:

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}
1
  • If nbytes is the amount of bytes you want to copy using memcpy, the third argument of memcpy should be nbytes without the end_idx. Commented Sep 15, 2015 at 15:14

2 Answers 2

5

You are copying the wrong amount in your code:

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

That should be just

memcpy(buffers.data_ + buffers.end_index, data, nbytes);
Sign up to request clarification or add additional context in comments.

9 Comments

You'll need to post a complete example to get better help.
char *data_ = new char[capacity_total]; has the same behavior
I was especially wondering about the code that allocates and initializes the Buffer object. If end_index were never initialized, we would expect it to be accidentally zero (and thus work) in the first simple cases, but might be accidentally negative when you create some Buffer object later. Once it is negative you can get the described symptom even with the other bug fixed. That is just a wild guess, since you didn't show that part of your code.
Yes I noted to. Big mistake... But... Still nothing.
If I understand you correctly, you are saying you also fixed the bug of failing to initialize end_index and that still leaves the failures. You added a little to the code you posted, but you fixed neither bug in that version and still don't show enough. That new code brings up the probability of a 3rd bug (in code you didn't show) that client will be 3 or more, which will also cause the symptom you report.
|
0

With the huge help of @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);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.