App is getting crashed randomly with error malloc: double free for ptr

There are close to 6000 JSON files I am downloading from backend. Later I will parse and insert the same records into RealmDB with below piece of code

let drugMonographURLs = [1.json,2.json,.......6000.json]
var drugs = [RLMDrug]()

drugMonographURLs.forEach { item in
            queue.async(group: group) {
                do {
                    let data = try Data(contentsOf: item)
                    let drug = try JSONDecoder().decode(RLMDrug.self, from: data)
                    drugs.append(drug)
                }
                catch {
                    print(error)
                }
            }
        }

At some point of time app is getting crashed immediately after this line

let drug = try JSONDecoder().decode(RLMDrug.self, from: data)

and getting error saying malloc: double free for ptr 0x7f8d4d1c5e00

Hi @Basavaraj_KM1,

Your code snippet is rather incomplete (what’s queue, for example?), and doesn’t seem to involve Realm at all: while presumably RLMDrug is an object that would ultimately end up in a Realm DB, within this specific sample all the objects are still unmanaged, so the SDK doesn’t seem to touch anything.

Last but not least, what’s your use case? What’s the rationale of having thousands of individual JSON files, instead of more manageable alternatives (for example, applying the data directly on the backend, and let Device Sync fill up the clients)?

Yes this issue we are getting before accessing Realm instance at the time of parsing the data

let drug = try JSONDecoder().decode(RLMDrug.self, from: data)