Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

c++ - Exception thrown: write access violation. this was nullptr

So I am trying to make a buffer class. This buffer class contains a huge buffer of size 384*4. The plan was for every UDP datagram received, size(384), the buffer class is called and return a pointer to where the datagram should be written.

And there will be another listener pointer to which RtAudio playback will memcpy from. [The listening part is not entirely relevant yet as I still have a problem writing into the buffer]

When I try to call server_->getPointer() (shown below), the "Exception thrown: write access violation. this was nullptr." is thrown. Please help me!! and tell me if there is anything else that I should provide.

Buffer.h

#pragma once

#ifndef BUFFER_H
#define BUFFER_H



class Buffer {
private:
    int bufferSize = 192 * 2; // one frame takes 2 Byte [int16]
    int nBuffers = 4;

    int *buffer_ = nullptr;

    int* writerPointer = nullptr;
    int* listenerPointer = nullptr;

    int writerCounter = 0;
    int listenerCounter = 0;

    int* tempW = nullptr;
    int* tempL = nullptr;

public:
    Buffer();
    ~Buffer();
    int* getWriterPointer();
    int* getlistenerPointer();
    int * getPointer();
};

#endif // !BUFFER_H

Buffer.cpp

#include"Buffer.h"
#include <iostream>


Buffer::Buffer() {
    buffer_ = reinterpret_cast<int*>(malloc(bufferSize*nBuffers));
    memset(buffer_, (int)5, bufferSize*nBuffers);

    std::cout << "new Buffer" << bufferSize * nBuffers << std::endl;
    listenerPointer = buffer_;
    writerPointer = buffer_;
    std::cout << "HERE " << *buffer_ << std::endl;
    std::cout << "new Buffer" << bufferSize * nBuffers << " pointer a " << listenerPointer << " pointer b " << writerPointer << std::endl;
}

Buffer::~Buffer() {
    delete buffer_;
}

...

//For teting purposes
int* Buffer::getPointer(){
    bufferSize = 192 * 2;
    std::cout << "get pointer asdfasdf::" << writerCounter << std::endl;
    std::cout << "pointer's position offset: " << writerCounter - 1 << std::endl;

    if (writerCounter == nBuffers - 1) {
        writerCounter = 0;
        return writerPointer + (bufferSize*(nBuffers - 1));
    }
    else {
        writerCounter += 1;
        return writerPointer + (bufferSize*(writerCounter - 1));
    }
}

main.cpp

#include <iostream>
#include "Buffer.h"


int main()
{
    std::cout << "Hello World!
"; 
    Buffer *buffer_ = new Buffer();

    buffer_->getPointer();


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Look up "zero copying" for the protocol part.

The problem you have is that your pointer is actually a nullptr at the time you are trying to use it. You need to check the return from malloc:

Buffer::Buffer() :
    buffer_(reinterpret_cast<int*>(malloc(bufferSize*nBuffers)))
{
    if(buffer_ == nullptr) throw std::bad_alloc();
}

But, you should use new instead which would do this check and throw bad_alloc automatically if it fails:

Buffer::Buffer() :
    buffer_(new int[bufferSize*nBuffers])
{
    // no need to check here
}

For every malloc you need one free, and for every new you need one delete - and you should never mix them.

Also, malloc allocates bytes, but an int is usually 4 or 8 bytes so you'd need to multiply the number of ints you want to allocate space for with sizeof(int)*CHAR_BIT/8 to get the correct size.

Just forget about malloc and free and use new and delete in their place.

This is how you delete your array allocated with new int[...]:

Buffer::~Buffer() {
    delete[] buffer_; // delete array
}

An even better option is to use a std_unique_ptr which will do the delete[] for you when it goes out of scope:

class Buffer {
private:
    std::unique_ptr<int[]> buffer_;
public:

    Buffer() :
        buffer_(std::make_unique<int[]>(bufferSize * nBuffers))
    {}
    // ~Buffer() no need to implement a destructor unless you manually handle resources
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...