简体   繁体   English

抛出异常:写访问冲突。 这是 nullptr

[英]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.这个缓冲区类包含一个大小为 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.该计划是针对每个接收到的 UDP 数据报,大小(384),调用缓冲区类并返回一个指向数据报应写入位置的指针。

And there will be another listener pointer to which RtAudio playback will memcpy from.并且会有另一个侦听器指针指向 RtAudio 播放将从中 memcpy。 [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.当我尝试调用 server_->getPointer() (如下所示)时,会抛出“ Exception thrown: write access violation. this was nullptr. ”。 Please help me!!请帮我!! and tell me if there is anything else that I should provide.并告诉我是否还有其他需要提供的。

Buffer.h缓冲区.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缓冲区.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!\n"; 
    Buffer *buffer_ = new Buffer();

    buffer_->getPointer();


}

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.您遇到的问题是您的指针在您尝试使用它时实际上是一个nullptr You need to check the return from malloc :您需要检查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:但是,您应该使用new代替它来执行此检查并在失败时自动抛出bad_alloc

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.每个malloc都需要一个free ,每个new都需要一个delete ——而且你永远不应该混合它们。

Also, malloc allocates bytes, but an int is usually 4 or 8 bytes so you'd need to multiply the number of int s you want to allocate space for with sizeof(int)*CHAR_BIT/8 to get the correct size.此外, malloc分配字节,但int通常是 4 或 8 个字节,因此您需要将要为其分配空间的int的数量与sizeof(int)*CHAR_BIT/8相乘以获得正确的大小。

Just forget about malloc and free and use new and delete in their place.忘记mallocfree并使用newdelete代替它们。

This is how you delete your array allocated with new int[...] :这是删除分配有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:一个更好的选择是使用std::unique_ptr当它超出范围时将为您执行delete[]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 抛出异常:写访问冲突。 temp 为 nullptr - Exception thrown: write access violation. temp was nullptr 抛出异常:写访问冲突。 这是 nullptr (C++) - Exception thrown: write access violation. this was nullptr (C++) 引发异常:写访问冲突。 newNode为nullptr - Exception thrown: write access violation. newNode was nullptr 抛出异常:读取访问冲突。 这是 nullptr - Exception thrown: read access violation. this was nullptr C++ 如何修复抛出的异常:写访问冲突。 这是空指针。 20号线 - C++ How do I fix Exception thrown: write access violation. this was nullptr. on Line 20 抛出异常:读取访问冲突。 这是对象数组中的 nullptr - Exception thrown: read access violation. this was nullptr in array of objects 抛出异常:读取访问冲突。 **pDSSearch** 为 nullptr - Exception thrown: read access violation. **pDSSearch** was nullptr 抛出C ++异常:读取访问冲突。 这是nullptr - C++ Exception thrown: read access violation. this was nullptr C ++-引发异常:读取访问冲突。 变量为nullptr - C++ - Exception thrown: read access violation. Variable was nullptr “抛出异常:读取访问冲突。 this-&gt;head 是 nullptr。” - “Exception thrown: read access violation. this->head was nullptr.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM