memoryswiftuistructcrashexc-bad-access

Thread 1: EXC_BAD_ACCESS (code=2, address=0x16bb7fff8) crashing only physical devices and not in Simulator while initiating items from Struct


I have an issue with my app crashing on physical devices while it tries to initiate the my cards. (simulator works fine)

I have a struct that basically initialises 61 cards like this:

extension HordeGameData {
 struct Cards {
        let SkeletonWarrior : MonsterCard = MonsterCard(id: 1,
                                                        name: "Skeleton Warrior",
                                                        image: "🥺",
                                                        strength: 6,
                                                        life: 1,
                                                        type:  .Undead,
                                                        location: .Plains,,
                                                        images: .init(Normal: "SkeletonWarriorNormal", Attack: "SkeletonWarriorAttack", Defend: "SkeletonWarriorDefend", Hit: "SkeletonWarriorHit", Dead: "SkeletonWarriorDead"),
                                                        scaleFactor: 1.1,
                                                        description: "A weak skeleton fighter."
        )
        
        let SkeletonArcher : MonsterCard = MonsterCard(id: 2,
                                                       name: "Skeleton Archer",
                                                       image: "🥳",
                                                       strength: 5,
                                                       fleeChance: 15,
                                                       giant: false,
                                                       type: .Undead,
                                                       location: .Plains,
                                                       images: .init(Normal: "SkeletonArcherNormal", Attack: "SkeletonArcherAttack", Defend: "SkeletonArcherDefend", Hit: "SkeletonArcherHit", Dead: "SkeletonArcherDead"),
                                                       scaleFactor: 1.1,
                                                       description: "A weak skeleton archer."
        )
... ... ...

if initiate more than 48 cards the game crashes with "EXC_BAD_ACCESS (code=2, ..." on load.

Is there a better way to store and call these cards when needed in game, which does not consume as much memory (I believe from other threats that this is a memory error).

I currently use:

HordeGameData.Cards.init().MonsterName

I use the same method for all other card types as well, therefore I should change the structure of how I store an load data.


Solution

  • Switching the Struct to Class prevents excessive memory usage and allows to add more items.

    Classes get only initiated once and are then referenced/pointed to for each call, while for each item in a Struct an individual memory space is generated for each initialisation.