LiYanan2004 / MarkdownView

Rendering Markdown text natively in SwiftUI.
https://liyanan2004.github.io/MarkdownView/documentation/markdownview/
MIT License
193 stars 18 forks source link

"Malloc: double free" issue when scroll #12

Closed hugo53 closed 1 year ago

hugo53 commented 1 year ago

Describe the bug Hello @LiYanan2004,

Thank you for the excellent package, I really appreciate it. I gave it a try and it seems quite impressive. However, I came across a problem as mentioned below:

(87710,0x16be9b000) malloc: double free for ptr 0x12d26f600
(87710,0x16be9b000) malloc: *** set a breakpoint in malloc_error_break to debug
(87710,0x16be9b000) malloc: Incorrect checksum for freed object 0x10d107a28: probably modified after being freed.
(87710,0x16be9b000) malloc: *** set a breakpoint in malloc_error_break to debug

Please see attached image for more details. Can you please help to check this problem? Thank you!

To Reproduce Steps to reproduce the behavior:

  1. Let have a scroll view of 100 MarkdownView elements, some elements have code block (in Swift or any languages).
  2. Scroll up and down several times, the crash will happen with above issue.

Expected behavior No crash when scroll.

Screenshots

Screenshot 2023-06-30 at 20 05 39

Environment:

LiYanan2004 commented 1 year ago

I'm facing this problem during this package development too. I'll try to fix that.

LiYanan2004 commented 1 year ago

MacBook Pro M1. Not sure whether my understanding is correct. This is a scrollview with 120 MarkdownView inside. Each one has a line of text or a code block.

But can't reproduce your crash. But I know this crash exist, so can you offer your code block content or your source code that can reproduce this crash?

Check out my code here:

https://github.com/LiYanan2004/MarkdownView/assets/37542129/c28090e4-596a-4fc6-a2b4-e907e284f29e

hugo53 commented 1 year ago

@LiYanan2004 Please check below code and video. I was able to reproduce the issue after 2 tries. Hope this help something.

import SwiftUI
import MarkdownView

struct ContentView: View {
    let text = """
    ```java
    public class Factorial {
        public static void main(String[] args) {
            int number = 5;
            int factorial = calculateFactorial(number);
            System.out.println("The factorial of " + number + " is: " + factorial);
        }

        public static int calculateFactorial(int num) {
            if (num == 0) {
                return 1;
            } else {
                return num * calculateFactorial(num - 1);
            }
        }
    }
"""

var body: some View {
    ScrollView {
        LazyVStack {
            ForEach(0..<150, id: \.self) { i in
                MarkdownView(text: text)
            }
        }
    }
}

}

struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }



https://github.com/LiYanan2004/MarkdownView/assets/1033299/572009e9-7d75-402c-9645-fc0c27f9dae5
LiYanan2004 commented 1 year ago

@hugo53 Please switch to the branch called fix-double-free-memory-crash and try it again. I may have fixed this issue.😁

hugo53 commented 1 year ago

Thank you for quick fix @LiYanan2004 . Seems it's better now but let me monitor for a while to confirm this on my side.

LiYanan2004 commented 1 year ago

OK. Glad to hear that. Don't forget to give me some feedbacks

hugo53 commented 1 year ago

@LiYanan2004 I tested many times with your fix and confirmed that it's working fine now. No crash anymore. Thank you again for the great fix.

LiYanan2004 commented 1 year ago

Okay. I’ll merge the fix. Thanks.

LiYanan2004 commented 1 year ago

1.4.0 released. I will delete that fix branch.

hugo53 commented 1 year ago

It's working fine now, I don't see the issue again. Thank you for great work!