iOS Question How to release bitmap

chjk

Member
Licensed User
Longtime User
object c code
B4X:
-(void)freeImage:(UIImage *)bitmap {
    //Get CGImage from UIImage
    CGImageRef img = bitmap.CGImage;
    //These two lines get get the data from the CGImage
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
   
    //clean up
    CFRelease(inBitmapData);
    CGImageRelease(img);
}

b4i code
B4X:
Public Sub changePic(bmp As Bitmap,w As Int,h As Int)As Bitmap
    Dim img As ImageView
    Dim f As Float
    img.Initialize("")
    img.Width = w
    img.Height = h
    Dim cvs As Canvas
    f= bmp.Height/bmp.Width
    cvs.Initialize(img)
    cvs.TargetRect.Initialize(0,0,w,f*w)
    cvs.DrawColor(Colors.White)
    cvs.DrawBitmap(bmp, cvs.TargetRect)
    Dim res As Bitmap = cvs.CreateBitmap
    cvs.Refresh
    'bmp=Null 'A memory leak occurs when the image is a lot.
     freeImageData(bmp)'this is error
    Return res
End Sub

Public Sub freeImageData(bmp As Bitmap)
    Dim no As NativeObject = Me
     no.RunMethod("freeImage:", Array(bmp))
End Sub
 

chjk

Member
Licensed User
Longtime User
The bitmap memory will be released automatically when there are no more live references to the bitmap.
The bitmap memory will be not released automatically, please look at the screenshot
s1.png

s2.png

s3.png

Repeated operation, memory will become very large,first memory 40MB,last memory 500MB, real bitmap will be not released automatically!
 
Upvote 0

chjk

Member
Licensed User
Longtime User
Please try to create a simple program that demonstrates this issue and upload it.
This is a simple program, Thanks!!!
And it there is no slide animation in iPad.
 

Attachments

  • Picdemo.zip
    315 KB · Views: 180
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code crashed whenever I tried to change the image.

Application_Start
Application_Active
Error occurred on line: 111 (Main)
Object was not initialized (UIImage)
Stack Trace: (
CoreFoundation __exceptionPreprocess + 180
libobjc.A.dylib objc_exception_throw + 50
CoreFoundation +[NSException raise:format:] + 141
B4i Example -[B4IObjectWrapper object] + 154
B4i Example -[B4IImageViewWrapper setBitmap:] + 118
B4i Example -[b4i_main _movenextani] + 2540
B4i Example -[b4i_main _touchfling::] + 2169
B4i Example -[b4i_main _page1_touch:::] + 1303
CoreFoundation __invoking___ + 29
CoreFoundation -[NSInvocation invoke] + 342
B4i Example +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 2069
B4i Example -[B4IShell runGoodChain::] + 293
B4i Example -[B4IShell raiseEventImpl:method:args::] + 3147
B4i Example -[B4IShellBI raiseEvent:event:params:] + 1623
B4i Example -[B4IPanelView touchesEnded:withEvent:] + 827
UIKit -[UIWindow _sendTouchesForEvent:] + 1095
UIKit -[UIWindow sendEvent:] + 1159
UIKit -[UIApplication sendEvent:] + 266
UIKit _UIApplicationHandleEventQueue + 7802
CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
CoreFoundation __CFRunLoopDoSources0 + 523
CoreFoundation __CFRunLoopRun + 1032
CoreFoundation CFRunLoopRunSpecific + 470
CoreFoundation CFRunLoopRunInMode + 123
GraphicsServices GSEventRunModal + 192
GraphicsServices GSEventRun + 104
UIKit UIApplicationMain + 160
B4i Example main + 138
libdyld.dylib start + 1
??? 0x0 + 1
)


If you think that there is a memory leak then try to create a simple program with minimum logic that reproduces it.
 
Upvote 0

chjk

Member
Licensed User
Longtime User
Your code crashed whenever I tried to change the image.

Application_Start
Application_Active
Error occurred on line: 111 (Main)
Object was not initialized (UIImage)

If you think that there is a memory leak then try to create a simple program with minimum logic that reproduces it.

Why didn't I crash here? I found that the memory of each change pic will increase by more than 2 Mb.
This is the modified a simple program
 

Attachments

  • Picdemo.zip
    223.5 KB · Views: 162
Upvote 0

chjk

Member
Licensed User
Longtime User
What is the purpose of Canvas here?
You can replace all this code with:
B4X:
IV1.ContentMode = IV1.MODE_FILL
The use of Canvas is for the image can be displayed in the original proportion,If the use of MODE_FILL images will be deformed!
 
Upvote 0
Top