Author Topic: Rare crash inside BitStream.Read  (Read 4814 times)

AlexT

  • Not-a-newbie
  • *
  • Posts: 4
  • Karma: 0
    • View Profile
Rare crash inside BitStream.Read
« on: May 10, 2011, 12:09:47 PM »
Hi,

I have a rare crash inside BitStream.Read when the system throws a AccessViolationException ("Attempted to read or write protected memory. This is often an indication that other memory is corrupt. "). This happens mainly when there's a lot of network traffic going on (sending and receiving maybe 10-20 messages each frame, 60 frames/second).

The main network message handler loop is fairly standard, i.e.

Code: [Select]
            Packet lPacket = mPeer.Receive();

            while (lPacket != null)
            {
                byte luPacketID = GetPacketIdentifier(lPacket);

                if( luPacketID < (byte) DefaultMessageIDTypes.ID_USER_PACKET_ENUM )
                {
                    HandleSystemNetworkMessage((DefaultMessageIDTypes) luPacketID, lPacket);
                }
                else
                {
                    HandleGameNetworkMessage((GameNetworkMessageID) luPacketID, lPacket);
                }

                mPeer.DeallocatePacket(lPacket);

                lPacket = mPeer.Receive();
            }

We use BitStream exclusively (no serialising structs). When reading bitstreams, I specify 'false' to not copy data, i.e.


Code: [Select]
            BitStream lIncomingBitstream = new BitStream(lPacket.data, lPacket.length, false);

            // Skip the message ID
            lIncomingBitstream.IgnoreBytes(sizeof(byte));

            switch (luPacketID)
            {
                case GameNetworkMessageID.ID_PLAYER_UPDATE_MESSAGE:
                    {
                        PlayerListMessage lPlayerListMessage = new PlayerListMessage();
                        bool lbOK = true;
                        lbOK &= lBitStream.ReadID(out miPlayerID);         // crashes here sometimes

                       // ....
                  }
                    break;

 I wondered if it could be a threading issue, as debugging it, I could reset the program counter and it would correctly read the data in on a second attempt.

Any help much appreciated.

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Rare crash inside BitStream.Read
« Reply #1 on: May 11, 2011, 12:05:34 PM »
BitStream isn't threadsafe. I'd have to add mutexes pretty extensively to the class to make it so. Is this something you need?

AlexT

  • Not-a-newbie
  • *
  • Posts: 4
  • Karma: 0
    • View Profile
Re: Rare crash inside BitStream.Read
« Reply #2 on: May 12, 2011, 06:47:57 AM »
Hi, thanks for the reply. If BitStream isn't threadsafe, I'm perhaps misinterpreting how it's meant to be used... The code posted was based on the RakNet samples and documentation.

How should an app using RakNet via SWIG (as a DLL) read & parse packets then? Is there a compile option to force the DLL to not run in a separate thread?

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Rare crash inside BitStream.Read
« Reply #3 on: May 12, 2011, 11:10:25 AM »
The posted code is fine, however the question is if you are using the same BitStream from multiple threads at the same time?

AlexT

  • Not-a-newbie
  • *
  • Posts: 4
  • Karma: 0
    • View Profile
Re: Rare crash inside BitStream.Read
« Reply #4 on: May 12, 2011, 11:13:59 AM »
No, it's a simple single-threaded XNA app. All my code runs single-threaded...

Does RakPeerInterface::StartUp spawn another thread? Would this be running at the same time as I'm calling Receive(), DeallocatePacket() and so on?

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Rare crash inside BitStream.Read
« Reply #5 on: May 13, 2011, 12:04:43 AM »
Yes, but that won't be the issue. The crash is something else. Next time it crashes can you give me a screenshot of the debugging along with a callback and relevant variables?

AlexT

  • Not-a-newbie
  • *
  • Posts: 4
  • Karma: 0
    • View Profile
Re: Rare crash inside BitStream.Read
« Reply #6 on: May 19, 2011, 03:56:35 AM »
Thanks for the help tracking this down.

My message loop was shutting down the peer when the host connection was lost, and then calling DeallocatePacket on it, which was throwing an exception (with good reason).