Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

iOS crashing after using WebCamTexture

Discussion in 'Scripting' started by d34thst4lker, Feb 19, 2015.

  1. d34thst4lker

    d34thst4lker

    Joined:
    Aug 4, 2013
    Posts:
    124
    For the longest time my app has been working without any issues and recently I noticed that it kept crashing after using the webcamtexture. This is the error I am getting in XCode;

    Code (CSharp):
    1. malloc: *** error for object 0xebd5aa0: pointer being freed was not allocated
    After I use the webcamtexture and store it using getPixels(), I then stop the cam and continue with my next page in the app but I have some Resources.unloadUnusedAssets around to clear up some textures but it seems when that gets called right after using the web cam, I receive the error above.

    I have been trying so many different things to get this done but it seems to be crashing only after I do texture2d.setPixels(webcamtexture.getPixels()) followed by the Resources.unloadUnusedAssets();

    This is very important and I need to get a fix for this asap.

    PS. I am using Unity 4.6.3 and this issue does NOT occur in Unity 4.6.1

    Thanks!
     
    Last edited: Feb 20, 2015
  2. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    770
    I'm experiencing the same thing - Unity 4.6.3. Also, the Webcam texture is actually flipped horizontally for me and seems to also have a small offset too. Very weird.

    You should post an example project in the bug tracker so the Unity team notices it.

    ... Just tested it on 4.6.2p1 and that was the last version that works fine. The issues above also occur in Unity 4.6.2p2 and Unity 4.6.3.
     
    Last edited: Feb 21, 2015
  3. d34thst4lker

    d34thst4lker

    Joined:
    Aug 4, 2013
    Posts:
    124
    Well for now I'm switching back to 4.6.1 so I don't have to worry about this issue. I have other things that need to be worked on at the moment
     
  4. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    770
    lol no worries - I just submitted an example project (Case 674503) - hopefully it will get fixed soon. :)
     
    d34thst4lker likes this.
  5. d34thst4lker

    d34thst4lker

    Joined:
    Aug 4, 2013
    Posts:
    124
  6. Dragonic8926

    Dragonic8926

    Joined:
    Mar 10, 2014
    Posts:
    34
    Some news for this problem? I was forced to downgrade to 4.6.0 for now because of that !
     
  7. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    770
    Not yet. First, Unity usually send an email to verify that they have tested the example project and have have recreated the bug - after that point they will start to work on it. Unfortunately I haven't received that yet so I assume they haven't even started working on a fix.

    I've found that Unity 4.6.2p1 is the last build with webcam functionality working reasonably well so I have had to roll back to that.
     
  8. d34thst4lker

    d34thst4lker

    Joined:
    Aug 4, 2013
    Posts:
    124
    Yea I am back on 4.6.2p1 as well being that 5.0 still doesn't have the issue fixed either. But 5.0 has many other things that mess up my project as well.
     
  9. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    770
    Yeah, its a little frustrating but not much we can do at this point. Unity 5 is pretty awesome on the whole but the worst thing about it so far is that baking light maps on big scenes doesn't work yet - at least not on the 2 scenes I've tested on. Hopefully Unity fix all of these issues soon :)
     
  10. yabea

    yabea

    Joined:
    Feb 26, 2015
    Posts:
    6
    Hi guys!

    We also have the issue where the WebCamTexture is flipped on iOS. In our case, it is vertically mirrored even though WebCamTexture.videoVerticallyMirrored says its not. I don't know about your projects, but ours is landscape only which might be why the feed is vertically flipped instead of horizontally.

    Anyway, we've tried the latest Unity version (4.6.3p3 as of now) and the issue is still there. Unfortunately we absolutely need to support x64 on iOS, so we can't revert to previous versions of Unity.

    Our last hope would be to completely ignore WebCamTexture.videoVerticallyMirrored on iOS and always flip the image but that feels like a kind of "duct tape" fix to me.

    Got any suggestions?

    Thanks!
     
  11. fireares

    fireares

    Joined:
    Mar 3, 2014
    Posts:
    9
    I kind of solved the problem by adding a simple script to the gameObject(applied webTexture), and I am using Unity 5 for my project. It should work as well for 4.6.x.
    The solution is simple, just create a new script called notDestroy, and add following code to it:

    voidAwake(){
    DontDestroyOnLoad (gameObject);
    }

    Then, attach it to the gameObject which is applied webCamTexture.
    As a result, the gameObject will not be destroyed after switching to the next scene. I know it could not be a good solution for memory management, but it works.
     
  12. fireares

    fireares

    Joined:
    Mar 3, 2014
    Posts:
    9
    By the way, the gameObject should not be under your main camera. It will be still destroyed with your main camera although you have that script.
     
  13. Smilediver

    Smilediver

    Joined:
    May 5, 2011
    Posts:
    72
    Hey guys. This issue is triggered the first time you call WebCamTexture.Stop() if you're using GetPixels() to get texture's content. After calling Stop(), Unity frees internal image buffer, but doesn't mark it NULL. So GC'ing WebCamTexture or calling Stop() again will crash the app, as well as calls to GetPixels() will write into already freed image, which might corrupt memory. There's no good workaround - the only way is to never call Stop() and don't GC WebCamTexture you've created. This affects both 4.6 and 5.0. For 4.6 you can downgrade to 4.6.2p1 (bug introduced in 4.6.2p2). We're working on the fix, and targeting coming patch releases.
     
  14. yabea

    yabea

    Joined:
    Feb 26, 2015
    Posts:
    6
    Hi guys,

    I've received more info regarding the "WebCamTexture is flipped" issue: it seems like it occurs only when using Metal graphics API, so forcing to Open GL ES 3.0 solves the issue until there's a proper fix.
     
  15. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    770
    Nope, happens to me on GL ES 2.0 too
     
  16. yabea

    yabea

    Joined:
    Feb 26, 2015
    Posts:
    6
    Here's what I've tested so far:
    • iPhone 4S
      • Open GL 2.0: WebCamTexture properly displayed on both landscape right & left
      • Open GL 3.0: same
      • Metal: same
    • iPhone 5S
      • Open GL 2.0: WebCamTexture properly displayed on both landscape right & left
      • Open GL 3.0: same
      • Metal: WebCamTexture always vertically mirrored (videoVerticallyMirrored always returns false)
    • iPhone 6
      • Open GL 2.0: WebCamTexture properly displayed on both landscape right & left
      • Open GL 3.0: same
      • Metal: WebCamTexture always vertically mirrored (videoVerticallyMirrored always returns false)
    It doesn't work on portrait nor portrait upside down, though. That might be why you still have the issue.
     
  17. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    770
    Ah, possibly - my setup is actually in Portrait mode. :)

    - Mel
     
  18. yabea

    yabea

    Joined:
    Feb 26, 2015
    Posts:
    6
    I've noticed that the image is always flipped in Portrait/Upside Down mode on all devices and on all Graphics API. I'm installing 4.6.2p1 at the moment to see if that was the case in this version, too.
     
    SAOTA likes this.
  19. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    770
    I believe 4.6.2p1 is the latest build that's workable , thats what I'm using right now at least! :)
     
  20. yabea

    yabea

    Joined:
    Feb 26, 2015
    Posts:
    6
    Hmm... I still have the issue 4.6.2p1... I've attached the script I'm using for testing if you want to compare with yours. :)
     

    Attached Files:

  21. Deleted User

    Deleted User

    Guest

    WebCamTexture.GetPixels() / GetPixels32() leaks memory too.

    http://issuetracker.unity3d.com/issues/ios-memory-leak-in-getpixels-and-getpixels32

    and, this code cause crash without webcamTexture.Stop(). (Unity 4.6.3f1 / 5.0.0f4 IL2CPP iOS)

    -------------------------------------
    void Update()
    {
    if (webcamTexture != null && webcamTexture.isPlaying) {
    webcamTexture.GetPixels32 (); // cause error​
    }​
    }
    -------------------------------------

    in Unity 4.6.1f1, this code works safety.I reported this bug.

    http://fogbugz.unity3d.com/default.asp?684028_j326ai4jfdk1jdv4
     
    Last edited by a moderator: Mar 30, 2015
  22. Smilediver

    Smilediver

    Joined:
    May 5, 2011
    Posts:
    72
    Memory leak was also fixed and it's in a pipeline targeting 4.6.4p2 and 5.0.1p1. Bug is in trampoline, so if it's critical for you, you can fix it by yourself. In Xcode find file "Classes/Unity/CameraCapture.mm", and at the end of the UnityCameraCaptureReadToMemory function add:
    Code (CSharp):
    1. ::free(tmpMem);
     
  23. Placeable

    Placeable

    Joined:
    Feb 6, 2014
    Posts:
    3
    I get the same results still using this fix. However I am now using the first Unity 5 patch, p1.
     
  24. Smilediver

    Smilediver

    Joined:
    May 5, 2011
    Posts:
    72
    There are 3 issues in total that were fixed:
    * Memory corruption or crash if you use GetPixels() and call Stop().
    * Memory leak in GetPixels(), leading to fast out of memory kill.
    * Corner case crash when Unity can't pick FPS, which I've seen on iPod iOS 6.1.5. Probably related to iOS version.

    So it could be that you're affected by the first issue. Is your app killed for using to much memory or it's a crash? Can you post a stack trace?
     
    fireares likes this.
  25. Placeable

    Placeable

    Joined:
    Feb 6, 2014
    Posts:
    3
    GetPixels() and SetPixels() are not used with the Texture. But Stop() is being called before switching scene. But I have also tried not calling on that method, same issue occurs then. Here's the Xcode crash received each and every time, also I want to stress this only started to happen when we upgraded to Unity 5 (Also with the latest April patch for it. April 2nd?):

    This is on an iPhone5S using iOS 8.1, see attachment.
     

    Attached Files:

  26. d34thst4lker

    d34thst4lker

    Joined:
    Aug 4, 2013
    Posts:
    124
    So, has this crash been fixed in 4.6.4? I don't think I'm ready to be moving up to 5 yet.
     
  27. Deleted User

    Deleted User

    Guest

    I tested WebCamTexture.Stop() and got this result.
    • 5.0.0f4 -> error
    • 5.0.0p3 -> error
    • 4.6.4p1 -> works! (camera image is flipped horizontally)
    • 5.0.1f1 -> error
    ::free(tmpMem); fix code is works good in each Unity versions :)
     
  28. d34thst4lker

    d34thst4lker

    Joined:
    Aug 4, 2013
    Posts:
    124
    Alright thanks! ill give it a try!
     
  29. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    4.6.4p2 tested and WebCamTexture.GetPixels32 still seems to leak memory.
     
  30. d34thst4lker

    d34thst4lker

    Joined:
    Aug 4, 2013
    Posts:
    124
    I just tested using 4.6.4p3 and I did not get the crash!! This is good news! Only problems I encountered was for the rear cam, I needed to swap the left/right direction for iOS. And secondly is the more important issue. When I call Stop() on the webcamtexture, my image on screen becomes black. (Apparently this was on the iPad I tested and not on the iPhone)

    When I used 4.6.2 I didn't have this issue. Anyone else?
     
    Last edited: Apr 21, 2015
  31. Eonirma

    Eonirma

    Joined:
    Apr 22, 2015
    Posts:
    2
    Hey Everyone,

    I am using webcamtexture on a project and after 1 week of search of how is it possible to make it work on ios with Unity 5.0.1, I have to ask here is this an internal issue to Unity ?

    I tried to use " ::free(tmpMem); ", I tried not to stop the webcam, I optimized my code for capture and it still don't work with Unity 5.0.1.

    It work well on windows with Unity 5 (no crash) and on IOS with unity 4.6.

    So is it a cross-compilation bug ?
     
  32. caardappel-hbs

    caardappel-hbs

    Joined:
    Mar 15, 2013
    Posts:
    5
    I've installed 4.6.4p1 - p4 and in none of those is the ::free(tmpMem); line found in the Trampoline code. Am I missing something? Was this fixed under the hood in some mysterious way? Am I installing patches wrong?
     
  33. piacentini

    piacentini

    Joined:
    May 27, 2014
    Posts:
    28
    Using 5.0.2f1 or 4.6.5p4 I still get crashes but I traced those to using GetPixels() on a Coroutine. Not sure if it is a bug or unintended usage, can someone clarify if this was supposed to work before I file a bug?
     
  34. d34thst4lker

    d34thst4lker

    Joined:
    Aug 4, 2013
    Posts:
    124
    I have not tried in 5 yet but using 4.6.5p4 everything seems fine. I hope 5 doesn't contain issues. That wouldn't be too great.
     
  35. taxvi

    taxvi

    Joined:
    Feb 18, 2013
    Posts:
    30
    thank you guys a lot! I managed to fix mine by invoking a method that stopped the webcamtexture after one second.

    if anyone is interested: I'm using Unity 5.0.1f1, Metalic API (Open ES 2.0 and 3.0 threw errors in xCode on launch but I did not pursue the reason). I'm trying to set a profile picture for my player. commenting out .Stop() was ok at first but if the player decided to retake the picture it was lagging S***less. so I'm calling .GetPixels() and then invoking another function containing the .Stop()

    EDIT:

    forgot to mention don't use Resources.UnloadUnusedAssets() or at least use it later on.

    ANOTHER EDIT:

    I tried to invoke Resources.UnloadUnusedAssets() after 2 seconds but it crashed again. so basically it's UnloadUnusedAssets() that's crashing my app after all :/
     
    Last edited: Jun 11, 2015
    PHARTGAMES likes this.
  36. PHARTGAMES

    PHARTGAMES

    Joined:
    Jan 30, 2013
    Posts:
    5
    Thankyou!

    I was experiencing a crash on pc, mac, ios, and android when calling WebCamTexture.Destroy() after WebCamTexture.GetPixels().

    I added a 1 second delay before calling WebCamTexture.Destroy() and a 1 second delay before calling WebCamTexture.Stop( ). (using coroutines)

    My GetPixels() call is not in a coroutine.

    The delays fixed the crash I was getting on WebCamTexture.Destroy() on pc editor (with unity remote), ios, and android.

    I am calling UnloadUnusedAssets much later in a different part of the app and i'm not getting a crash there.
    (in 5.0.2f1, ES2.0)
     
    taxvi likes this.
  37. pastaHead2

    pastaHead2

    Joined:
    Dec 24, 2014
    Posts:
    7
    I faced with annoying crash on my iPad Mini 2 while using WebCamTexture (Unity5.3.4p4)

    This is my case:
    1) Create 2 WebCamTextures (front and back cameras)
    2) invoke Play() of first WebCamTexture
    3) start coroutine, where I'm waiting until this texture is initialized (checking if width is more than 100), according to this topic: http://answers.unity3d.com/answers/1148424/view.html
    4) invoke GetPixels32()
    5) doing some stuff
    6) switch cameras (invoke Stop() of first one, and Play() of second one)
    7) waiting until the second one is initialized
    8) invoke GetPixels32()
    9) doing some stuff
    10) switch cameras again
    11) waiting until the first one is initialized again
    12) invoke GetPixels32() results in crash on iOS only

    One important note is that if I add 0.5 seconds waiting after invoking Play() method before invoking GetPixels32() - it doesn't crash, so looks like the above solution of waiting until texture width is more than 100 doesn't work in all cases.

    Anyway - I can't really rely on this solution of waiting for 0.5 seconds, since you never know if it's enough for all devices.
     
  38. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,969
    Have you tried using just one WebCamTexture and setting the name when you want to switch cameras? Also, what is your stack trace?
     
  39. iamsidv

    iamsidv

    Joined:
    Jan 24, 2013
    Posts:
    17
    Bump again.. is this issue fixed?