C CaptKronos Active Member Licensed User Longtime User Mar 4, 2019 #1 I am probably doing something silly here but should the following code work? Depending on the bitmap's dimensions the loop crashes after a consistent number of iterations. The larger the dimensions, the fewer the iterations before it crashes. (With 1280x720 it crashes after 394 iterations on an iPhone SE.) I assume there is some memory issue at play. Nothing is written to the log when it crashes, the app simply closes. I have never seen it crash on the simulator but that's perhaps due to the amount of memory available? I have included a couple of comments in the code just to show that the real code is doing something more interesting than writing a black bitmap on top of another 1000 times! Code: Copy bc1.Initialize(1280,720) bc1.DrawRect(bc1.TargetRect,Colors.Black,True,1) Dim bmp As B4XBitmap=bc1.Bitmap bc2.Initialize(1280,720) For i=1 To 1000 'generate a new bmp bc2.DrawBitmap(bmp, bc2.TargetRect, True) 'write bc2.bitmap Log(i) Next
I am probably doing something silly here but should the following code work? Depending on the bitmap's dimensions the loop crashes after a consistent number of iterations. The larger the dimensions, the fewer the iterations before it crashes. (With 1280x720 it crashes after 394 iterations on an iPhone SE.) I assume there is some memory issue at play. Nothing is written to the log when it crashes, the app simply closes. I have never seen it crash on the simulator but that's perhaps due to the amount of memory available? I have included a couple of comments in the code just to show that the real code is doing something more interesting than writing a black bitmap on top of another 1000 times! Code: Copy bc1.Initialize(1280,720) bc1.DrawRect(bc1.TargetRect,Colors.Black,True,1) Dim bmp As B4XBitmap=bc1.Bitmap bc2.Initialize(1280,720) For i=1 To 1000 'generate a new bmp bc2.DrawBitmap(bmp, bc2.TargetRect, True) 'write bc2.bitmap Log(i) Next
Erel B4X founder Staff member Licensed User Longtime User Mar 4, 2019 #2 How are you generating the new bitmap? It crashes because the memory will only be released after the code completes. Upvote 0
How are you generating the new bitmap? It crashes because the memory will only be released after the code completes.
C CaptKronos Active Member Licensed User Longtime User Mar 4, 2019 #3 Sorry, my text is confusing. The tests don't include the code to generate the new bmp. It crashes purely with: Code: Copy For i=1 To 1000 bc2.DrawBitmap(bmp, bc2.TargetRect, True) Log(i) Next Upvote 0
Sorry, my text is confusing. The tests don't include the code to generate the new bmp. It crashes purely with: Code: Copy For i=1 To 1000 bc2.DrawBitmap(bmp, bc2.TargetRect, True) Log(i) Next
Erel B4X founder Staff member Licensed User Longtime User Mar 4, 2019 #4 This code is expected to crash because of the reason I posted above. My question is important. If you can use DrawBitmapCreator instead of DrawBitmap then the performance will be significantly better. Upvote 0
This code is expected to crash because of the reason I posted above. My question is important. If you can use DrawBitmapCreator instead of DrawBitmap then the performance will be significantly better.
C CaptKronos Active Member Licensed User Longtime User Mar 4, 2019 #5 I thought you meant that the "generate a new bmp" code was not releasing memory. I still don't understand why having the one bitmap generated before the loop starts and then copying it to a BitmapCreator (1000 times) should generate any memory issues. However, I can confirm that the following works Code: Copy for i=1 to 1000 bc2.DrawBitmapCreator(bc1, bc1.TargetRect,0,0, True) log(i) next The reason I didn't take that route initially was, once I get to the real code, I would have to convert the bitmap, from "getNextBitmap", to a BitmapCreator and hence run into the memory issues again. Something like: Code: Copy For i=1 To 1000 bmp=getNextBitmap bmpRect.Initialize(0,0,bmp.Width,bmp.Height) bc1.DrawBitmap(bmp, bmpRect, True) bc2.DrawBitmapCreator(bc1, bc1.TargetRect,0,0, True) Log(i) Next I guess what I need to do is instead of having a "getNextBitmap", I create a "getNextBitmapCreator" and then its output can be used directly by DrawBitmapcreator. Upvote 0
I thought you meant that the "generate a new bmp" code was not releasing memory. I still don't understand why having the one bitmap generated before the loop starts and then copying it to a BitmapCreator (1000 times) should generate any memory issues. However, I can confirm that the following works Code: Copy for i=1 to 1000 bc2.DrawBitmapCreator(bc1, bc1.TargetRect,0,0, True) log(i) next The reason I didn't take that route initially was, once I get to the real code, I would have to convert the bitmap, from "getNextBitmap", to a BitmapCreator and hence run into the memory issues again. Something like: Code: Copy For i=1 To 1000 bmp=getNextBitmap bmpRect.Initialize(0,0,bmp.Width,bmp.Height) bc1.DrawBitmap(bmp, bmpRect, True) bc2.DrawBitmapCreator(bc1, bc1.TargetRect,0,0, True) Log(i) Next I guess what I need to do is instead of having a "getNextBitmap", I create a "getNextBitmapCreator" and then its output can be used directly by DrawBitmapcreator.
Erel B4X founder Staff member Licensed User Longtime User Mar 4, 2019 #6 Always prefer BitmapCreator over B4XBitmap when working with BitmapCreator. bc.DrawBitmap creates a BitmapCreator for you every call. Upvote 0
Always prefer BitmapCreator over B4XBitmap when working with BitmapCreator. bc.DrawBitmap creates a BitmapCreator for you every call.