Unhandled exception at 0x00051526 Access violation reading location 0x00000004

12,023

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.

Share:
12,023
Abanoub
Author by

Abanoub

I am (Polyglot/Full-Stack) Developer able to build a Web/Mobile presence from the ground up - from concept, navigation, layout and programming to UX. Skilled at writing well-designed, testable and efficient code using current best practices in development. Fast learner, hard worker and team player who is proficient in an array of languages and tools. My formal field of study was on the English language at The Higher Institute of Language, however I have spent several thousands of hours over the past 10 years practicing and mastering a variety of programming languages. Programming is both my passion and my career choice - for this reason I can offer my dedication and passion in software engineering on a variety of platforms in your projects.

Updated on June 04, 2022

Comments

  • Abanoub
    Abanoub almost 2 years
    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!?