0

I need to catch a fatal error thrown from this code, the error is bellow,

let contents: Any?
do {
    contents = try Cstes.bufferECG_A.append(tmpECG)
} catch _ {
    contents = nil
    print("Catch the problem in Cstes.bufferECG_A")
}

error :

malloc: *** error for object 0x1c5430c80: pointer being freed was not 

allocated
*** set a breakpoint in malloc_error_break to debug

and the swift compiler tells me the advice bellow:

'catch' block is unreachable because no errors are thrown in 'do' block

How can I make it work properly?

Thomas Goyne
  • 8,010
  • 32
  • 30
gxmad
  • 1,650
  • 4
  • 23
  • 29
  • 2
    Possible duplicate of https://stackoverflow.com/questions/38737880/uncaught-error-exception-handling-in-swift – You *cannot* catch arbitrary runtime exceptions in Swift. – Martin R Oct 20 '17 at 07:59
  • 2
    You make it "work properly" by fixing the code which caused the memory corruption. – Martin R Oct 20 '17 at 08:01
  • So I cannot just say, it is an a memory error, catch it ? – gxmad Oct 20 '17 at 08:35
  • No. It's in general not possible to recover from memory errors. Swift is memory-managed so, as a general rule, you should never see such errors -- unless you mess up (by using unsafe API without being very careful). – Raphael Oct 20 '17 at 09:56

0 Answers0