William Alex
William Alex

Reputation: 415

iOS Swift : EXC_BAD_ACCESS(code=EXC_i386_GPFLT ) regarding a singleton

My iOS app is returning this error.

EXC_BAD_ACCESS(code=EXC_i386_GPFLT )

This is occuring on return Singleton.instance Here is the code regarding the singleton I am using.

class var sharedData : SharedData {
    struct Singleton {
        static let instance = SharedData()
    }

    return Singleton.instance
}

Can someone help me understand this error and help me resolve it? Any suggestions or tips are appreciated it.

Upvotes: 1

Views: 2126

Answers (4)

endavid
endavid

Reputation: 1963

I was using a singleton as others have mentioned above,

static let sharedData = SharedData()

and it was crashing on a real device but not in the simulator. It turns out that I just needed to clean the project and rebuild. Don't fall for false positives ;)

Upvotes: 2

Dov
Dov

Reputation: 16176

I had a badly named function in my Swift singleton class, that must have been tripping up ARC when it was called. This class initializes another class from a file, and so I ended up with this signature:

func initOtherClass(otherClass: NSObject, URL fileURL: NSURL) -> Bool

Whoops. Changing the name from init to initialize solved the EXC_BAD_ACCESS errors. I hope this helps to save someone else some time.

Upvotes: -1

HorseT
HorseT

Reputation: 6592

With Swift 1.2 there is an easier option to create singletons now:

class DataManager {
    static let sharedInstance = DataManager()

    /// To deny direct access, make your init function private if you want
    private init() {
    }
}

Upvotes: 2

bzz
bzz

Reputation: 5596

You can replace all your code with the following:

static let sharedData = SharedData()

Upvotes: 1

Related Questions