Skip to content

System.AccessViolationException #3

Open

Description

@Chester-Gould

Thank you for creating this .NET implementation of Spout2. I'm not sure if it's my configuration or what, but I believe I've followed the instructions correctly and I am getting the following Error, at "sender.SendImage" when I try run it.

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

Regularly I've seen that if the architecture was incorrect, but my build config is set to x64 per the ReadMe.

Activity

  1. HolgerFoerterer commented on Oct 22, 2022

    @HolgerFoerterer

    I get the same issue here. Could you resolve the issue in February?

  2. kinsi55 commented on Feb 16, 2023

    @kinsi55

    I get this too but at random. Some times I do, other times I dont. I'll try to figure out if theres a way to workaround it

    Edit: Took a look and there is no consistent way to detect that it is broken. some times some values of the Spout instance are 01010101.... when its broken but not always. Some some times SendImage will not fail, but only send a black image anyways.

    Something has to be going wrong in the construction phase of something because when it does work once, reusing the same instances will consistently work 100% of the time.

  3. YaF3li commented on Jun 3, 2023

    @YaF3li

    I have a similar issue. Although from the testing I have conducted on my machine so far, it seems that I can reliably make it work or crash. If I set Serilog to output to console Spout works, at least I did not yet have it throw the exception in this case. If I set it to output to a file, it will throw, also reliably as of my current observations.
    I presume the different logging outputs change something about the timing, but I could not narrow it down any further as of yet.

  4. YaF3li commented on Jun 7, 2023

    @YaF3li

    I was unable to find an exact cause, but I have switched from using a managed byte array to using memory obtained by Marshal.AllocHGlobal. This has seemingly resolved my issue.

  5. kinsi55 commented on Jun 7, 2023

    @kinsi55

    I already am kind of doing that - My sent pixels are obtained via LockBits

  6. YaF3li commented on Jun 7, 2023

    @YaF3li

    Specifically what I am now doing that seems to work is allocate using AllocHGlobal at the start of the program and retaining the memory, then LockBits of the bitmap, and copy that over into the allocated memory using Buffer.MemoryCopy, then UnlockBits and finally passing the pointer of the allocated memory to SendImage.

  7. kinsi55 commented on Jun 7, 2023

    @kinsi55

    Seems like we're having different issues then (See my first comment above). This didnt fix my problem FWIW

  8. nickhudson4 commented on Dec 11, 2023

    @nickhudson4

    For me this happens when I try to convert my bitmap to a JPEG byte array like so

    using (var memoryStream = new MemoryStream())
    {
        bitmap.Save(memoryStream, ImageFormat.Jpeg);
        _newFrame = memoryStream.ToArray();
    }
    

    The solution was to change to Bmp format

    using (var memoryStream = new MemoryStream())
    {
        bitmap.Save(memoryStream, ImageFormat.Bmp);
        _newFrame = memoryStream.ToArray();
    }
    
  9. JanMisker commented on Sep 10, 2024

    @JanMisker

    Specifically what I am now doing that seems to work is allocate using AllocHGlobal at the start of the program and retaining the memory, then LockBits of the bitmap, and copy that over into the allocated memory using Buffer.MemoryCopy, then UnlockBits and finally passing the pointer of the allocated memory to SendImage.

    It's been a while, but I'm facing this issue as well.
    @YaF3li could you share the relevant parts of your code please?

  10. YaF3li commented on Sep 10, 2024

    @YaF3li

    @JanMisker These should be the relevant extracts:

    // Init:
    int bufferLength = size.Width * size.Height * 4; // Assume alpha channel
    IntPtr bufferPtr = Marshal.AllocHGlobal(bufferLength);
    
    // Send Loop:
    // (must declare unsafe)
    BitmapData bmpData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
    int numbytes = bmpData.Stride * bitmap.Height;
    if(numbytes == bufferLength)
    	Buffer.MemoryCopy((void*)bmpData.Scan0, (void*)bufferPtr, numbytes, numbytes);
    bitmap.UnlockBits(bmpData);
    sender.SendImage((byte*)bufferPtr, size.Width, size.Height, GL_RGBA, false, 0);
    
    // Dispose:
    Marshal.FreeHGlobal(bufferPtr);
    
  11. JanMisker commented on Sep 10, 2024

    @JanMisker

    Thanks so much @YaF3li
    I was doing some more debugging and in the end I managed to do it without an extra buffer, i.e. it's something like

    BitmapData bmpData = bitmap.LockBits(...);
    sender.SendImage((byte*)bmpData.Scan0, ... );
    bitmap.UnlockBits(bmpData);
    bitmap.Dispose();
    

    And this works...

  12. kinsi55 commented on Sep 10, 2024

    @kinsi55

    @JanMisker I also eventually came up with that implementation and that does still error out occasionally for me - Tho it worked more consistently from what I remember

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions