抛出异常:写访问冲突.这是 nullptr [英] Exception thrown: write access violation. this was nullptr

查看:47
本文介绍了抛出异常:写访问冲突.这是 nullptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试创建一个缓冲区类.这个缓冲区类包含一个大小为 384*4 的巨大缓冲区.计划是针对接收到的每个 UDP 数据报大小(384),调用缓冲区类并返回一个指向数据报应写入位置的指针.

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.

还有另一个侦听器指针,RtAudio 播放将从中进行 memcpy.[听力部分尚不完全相关,因为我仍然有写入缓冲区的问题]

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]

当我尝试调用 server_->getPointer() (如下所示) 时,会抛出异常抛出:写访问冲突.这是 nullptr.".请帮我!!并告诉我是否还有其他需要我提供的东西.

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.

缓冲区.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

缓冲区.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();


}

推荐答案

协议部分查找零拷贝".

Look up "zero copying" for the protocol part.

您遇到的问题是您的指针在您尝试使用它时实际上是一个 nullptr.您需要检查 malloc 的返回:

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

但是,您应该使用 new 来代替,它会执行此检查并在失败时自动抛出 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
}

对于每个 malloc 你需要一个 free,对于每个 new 你需要一个 delete - 你永远不应该混合它们.

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

此外,malloc 分配字节,但 int 通常是 4 或 8 个字节,因此您需要乘以 int 的数量你想用 sizeof(int)*CHAR_BIT/8 分配空间以获得正确的大小.

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.

忘记mallocfree 并使用newdelete 代替它们.

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

这是删除分配给 new int[...] 的数组的方法:

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

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

更好的选择是使用 std_unique_ptr ,它会在超出范围时为您执行 delete[] :

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

这篇关于抛出异常:写访问冲突.这是 nullptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆