引发异常:写访问冲突.这是nullptr [英] Exception thrown: write access violation. this was 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播放将从该侦听器指向. [聆听部分还不完全相关,因为我在写入缓冲区时仍然遇到问题]
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.
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
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!\n";
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 int
s you want to allocate space for with sizeof(int)*CHAR_BIT/8
to get the correct size.
只需忘记 malloc
和 free
,并在其位置使用 new
和 delete
.
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屋!