开发者

Unhandled exception at 0x00051526 Access violation reading location 0x00000004

class ByteBuffer
{
public:
    ByteBuffer(std::shared_ptr<uint8_t> buf, int len);
    explicit ByteBuffer(int len);
    virtual ~ByteBuffer(void);

    std::shared_ptr<uint8_t> getBuffer() const {return this->buffer;}
    uint16_t getLength() const {return this->length;}
private:
    std::shared_ptr<uint8_t> buffer;
    uint16_t length;
};

//-----------------------------------------------------------------------------
ByteBuffer::ByteBuffer(std::shared_ptr<uint8_t> buf, int len)
  : buffer(buf),length(len)
{ }

ByteBuffer::ByteBuffer(int len)
  : buffer(new uint8_t[len]),length(len)
{ }

ByteBuffer::~ByteBuffer(void)
{ }

//-----------------------------------------------------------------------------
class Packet
{
public:
    explicit Packet(ByteBuffer& ref);
    virtual ~Packet(void);
};

Packet::Packet(ByteBuffer& ref)
{开发者_运维技巧
    // how do i intinlize it here so i can use it?
}

// i have onther method for the handling 
void HandlePacket(Packet & pack);
Handel(ByteBuffer & ref)
{
    Packet p(ref);
    HandlePacket(p); // the error happens here
}

Edit: sorry i forgot to add the Method where the error happens my bad sorry

as you can see the 2calsss, but every time am trying to pass the bytebuffer inside the packet then use the packet inside onther method it gives me this error:

Unhandled exception at 0x00051526 in AccountServer.exe: 0xC0000005: Access violation reading location 0x00000004.

so my question how can i solve this prob!?


What's happening is you are accessing the address 0x4. Probably some object is NULL and you've tried to de-reference it with something like ptr[1] or the -> operator.

Run your program under a debugger and it will be clearer what's happening. In particular it will give you a stack trace and tell you about the state of local variables.

As is mentioned in the comments, you can't use new [] with shared_ptr in the way that you're expecting, since delete is different from delete []. See this website, which came up in a Google search: http://nealabq.com/blog/2008/12/02/array_deleter/ . You will need a customer deleter which does delete [] instead of the default, which is just delete.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜