Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

Error"Attempted to read or write protected memory"

I’m attempting to overwrite a texture using the Texture2D LoadImage method. Using the following lines of code:

WWW _web = new WWW();
Texture2D t = new Texture2D(640, 480);
_web.LoadImageIntoTexture(t);
((Texture2D)this.GetComponent().material.mainTexture).LoadImage(t.EncodeToJPG());

Everything works when I run the application in the unity debugger but when I attempt to run the app on the HoloLens emulator or on the device itself in both debug and release modes I get the following exception:
System.AccessViolationException was unhandled
HResult=-2147467261
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=UnityEngineProxy
StackTrace:
at UnityEngineProxy.InternalCalls.PInvokeCalls.Texture2D_CUSTOM_LoadImage(IntPtr param_0, Int64 param_1, Boolean param_2)
at UnityEngineProxy.InternalCalls.Texture2D_CUSTOM_LoadImage(Object self, Object data, Boolean markNonReadable)
at UnityEngine.Texture2D.LoadImage(Byte[] data) .....

Is there a better way to dynamically retrieve an image and displayed it in the scene?

Tagged:

Best Answer

  • Accepted Answer

    @Malcolm Not sure what this script is attached to but the back and forth between texture and image on the last line doesn't seem necessary. Here's a snippet that works for me:

    IEnumerator Start () {
        var www = new WWW("http://apod.nasa.gov/apod/image/1607/MagCloudsDeep_BeletskyEtAl_960.jpg");
        yield return www;
    
        var tex = new Texture2D(500, 500);
        if (www.isDone)
        {
            www.LoadImageIntoTexture(tex);
            gameObject.GetComponent<Renderer>().material.mainTexture = tex;
        }
    }
    

Answers

  • WWW does work in the background. You must yield return the result or wait for .isDone to be true.

    See example here https://docs.unity3d.com/ScriptReference/WWW.LoadImageIntoTexture.html

  • Thanks @WithinRafael. I actually am checking the isDone property I just didnt include it with the code snippet for brevity. Here is the block of code that includes the isDone check:

                // load the image if it is ready
                if (this._web != null && this._web.isDone)
                {
                    UnityEngine.Debug.Log(String.Format("Loading new image [{0}]...", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff")));
                    Texture2D t = new Texture2D(640, 480);
    
                    this._web.LoadImageIntoTexture(t);
    
                    ((Texture2D)this.GetComponent<Image>().material.mainTexture).LoadImage(t.EncodeToJPG());
    

    UnityEngine.Debug.Log("New material set...");
    }

  • @Malcolm Ah, cool. Will update my test case to closely match yours and see what's going on.

  • Accepted Answer

    @Malcolm Not sure what this script is attached to but the back and forth between texture and image on the last line doesn't seem necessary. Here's a snippet that works for me:

    IEnumerator Start () {
        var www = new WWW("http://apod.nasa.gov/apod/image/1607/MagCloudsDeep_BeletskyEtAl_960.jpg");
        yield return www;
    
        var tex = new Texture2D(500, 500);
        if (www.isDone)
        {
            www.LoadImageIntoTexture(tex);
            gameObject.GetComponent<Renderer>().material.mainTexture = tex;
        }
    }
    
  • Sorry it took so long to get back to you. Your solution worked like a charm.

Sign In or Register to comment.