[英]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.忘记
malloc
和free
并使用new
和delete
代替它们。
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.