iPhone crash for feeding base64 to webview

2019-08-01 04:37发布

问题:

I am using UIImagePickerController to choose image/video,upon selection I am converting the resource into base64 string and I am sending that to wkwebview

NSData(contentsOfURL:(info[UIImagePickerControllerMediaURL] as? NSURL)!)?.base64EncodedDataWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

I am sending the base64 string to webview using evaluatejavascript

Here is the JS function

function showResource(base64,type){
        if (type == "image"){
            document.getElementById("div1").innerHTML="<img src='data:image/jpeg;base64,"+base64+"' width='100' height='100' alt='No Image'/>";
        }
        else{
            document.getElementById("div1").innerHTML="<video width='320' height='240' controls><source src='data:video/x-m4v;base64,"+base64+"'></video>";
        }
}.

I am facing the following problems

  • sometimes the webpage that is in webview becomes blank
  • sometimes the application gets crashed
  • sometimes the device goes into multiple reboot

I couldn't find any memory leak in native code.I tried Instruments.

  • try loading a video of 25 seconds long first time it won't crash and you can't find a memory leak in Instruments
  • try loading the same video again the results are same
  • try doing the same,this time you'll see the device has gone offline

    or if we try to load a video that's 60 longs it will crash the first time itself.

I am not sure what's causing the problem.

As the page is turning blank , it could be a javascript memory leak I thought

But I don't think the above function can cause memory leak since we're reassigning the new base64 on the same variable the older base64 should have been released from the memory and moreover javascript is garbage collected language.

so this is contradicting.

so base64 conversion might take more memory which in turn causes iOS to crash the app or crash the iPhone also in worst case but if this is the case why sometime html page is turning blank.

so this is also contradicting.

Any help is appreciated !

Update:

So far in my research it shows the problem is with memory leak in javascript.

回答1:

If xcode print "pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug" ,try to turn off the safari web viewer.