Newbie and getting a error with struct data access

Help please!! I have such struct:

struct Config {
	var logging: Bool
	var isServer: Bool
	var isVerbose: Bool
	var isVirtualMachine: Bool
  init() {
    logging = false
    isServer = false
    isVerbose = false
    isVirtualMachine = false
  }
}
var config = Config.init()

The values for config are loaded in the main file. There at the main file can read it all, but here begins my trouble, that I am trying to figure out about a week (actually I found this point as the offender line just today). Somewhere in another file of the project when trying to read a property of that struct it will crash with a trace trap error.

The weird is that is happening, so far, only with SOME, not all, Mac OS Monterey versions and I all saw that if Xcode is installed on the Monterey the program will work (but this is not science proof). On my notebook running Mac OS Monterey, without Xcode it works! Other client's computer it will crash, and so on.

So, it will run without any errors with some macOS but will crash in others.

I really don't know how to fix it or what can be done. This program was running since 2019 and after I need to make some changes my client asked for this is happening. I wasn't in touch with Swift since that time, so I forget and lost something here.

Somewhere in another file of the project when trying to read a property of that struct it will crash with a trace trap error.

Where exactly do you call var config = Config.init() ?

Is it where the crash occurs ?

Please show some code where this crashes. And tell exactly where it crashes, what is the error message.

Note, in this simple case you could replace with:

struct Config {
    var logging = false
    var isServer = false
    var isVerbose = false
    var isVirtualMachine = false
}
var config = Config()

Hi Claude, thanks a lot for the reply!!!

I already had tried that way you show, without the constructor, simply: var config = Config()

Same thing! So I moved to the implicit init construct. Well in my code I have a function that output to another struct (Instrumentation) such:

func getInstrumentation() -> Instrumentation {
   var instrumentation: STInstrumentation
  /* do some simple stuff here like declare other vars
  ... put the need values to function and one of that information is, eg., isVirtualMachine, but this data comes from the Config
  .... so it will crash right here whatever use/call it do >>> */

 if config.isVirtualMachine { do something } // will crash here if this is the 1st line
 instrumentation.isVirtualMachine = config.isVirtualMachine // **this is the actual line in my code where it crashes:(**
 
}

That "if config" is just an example, the real code uses the line below, the only way I had to search for this error was doing that, as in my computer it won't raise any error and I cant make remote debug (I don't even know if Swift does that). I am telling that line crashes but any attempt to read any of the config properties will make it fail.

EDIT: I made a change and sent to the client to him a try (waiting for the answer), I change the function declaration to func getInstrumentation(configInformation: STConfig) -> STInstrumentation and now I am passing the config as parameter. Hope it can fix. Anyway fixed or not I really can't understand why with some devices it works.

Thanks. Effectively, replacing init() by an in line = false should not solve the issue, it is equivalent and just simpler.

You do not show where you initialise config. Root cause is elsewhere. Please show more code. As well as crash message and crashlog.

Show the complete code of func getInstrumentation() ,

as well as definitions of Instrumentation and STInstrumentation

Also, could you add a print:

 print("config", config)
 if config.isVirtualMachine { do something } // will crash here if this is the 1st line
 instrumentation.isVirtualMachine = config.isVirtualMachine // **this is the actual line in my code where it crashes:(**

.

I really can't understand why with some devices it works.

First, need to confirm this behavior. What are the devices where it works / doesn't work ? What are their iOS versions…

That could be a timing issue: either config is not yet allocated or is no more existing when you call… But need to see more code to find out. and tell what you get.

When I say other devices I mean about other Mac OS with same or different versions, Big Sur, Monterey, etc. Some it will give the error and a lot of are running ok. I will try simplify the code, using only these properties of the Config, as it is where we get the error whatever property I try to access (the actual struct have a lot of other properties):

struct STInstrumentation: Codable {
    var logging: Bool = false
    var isServer: Bool = false
    var isVerbose: Bool = false
    var isVirtualMachine: Bool = false
    mutating func reset() {
		self = STInstrumentation()
	} 
}
struct Config {
    var logging = false
    var isServer = false
    var isVerbose = false
    var isVirtualMachine = false
}

func getInstrumentation() -> STInstrumentation {
   var instrumentation: Instrumentation 

  // while copying from the real code to here I notice that this local instrumentation isn't initialized...
  instrumentation = STInstrumentation.init() // << this line is not present into my code, just put it now
  
  // config var struct is global, but right now I am sending it as parameter
  // STInstrumentation() receives more parameters, but only the config will raise the exception
  // this error occurs even if before there like the example before
  if config.isServer { print("wont print, it will crash") }
  instrumentation = STInstrumentation(logging: config.logging, isServer: isServer: config.isServer, 
     isVerbose: config.isVerbose, isVirtualMachine: config.isVirtualMachine)
  
  return instrumentation
}

var config = Config() // global config var
var Instrumentation: STInstrumentation // global Instrumentation var

Instrumentation = STInstrumentation.init()

// the information to the Config is from a SQLite, no issues here
config.logging = true
config.isServer = true
config.isVerbose = true
config.isVirtualMachine = false;

 Instrumentation = getInstrumentation()
 // ... from here the program continues with others stuff to do, including the use of the Instrumentation.

The terminal (this software is for terminal) will end with: zsh: trace trap /Applications/MyApplication

I still have to send to my client to him test with that init() but why it didn't crash before? By the way, the crash occurs only when trying to read the config value, this is why I modified to pass as parameter. The code you see here is such as what he has in hands now.

Thank you again and forgive me for my English too.

I'm not familiar with code for Terminal, but I guess config is not or no more defined when you use it.

What is the error message ?

Did you try to print(config) to see what it is ?

Print the config is all there, no issues!

> Config(logging: false, isServer: false, isVerbose: false, isVirtualMachine: false, isVip: false, endpoint: "gateway.xxxx.com", endpoint_scheme: "https", endpoint_path: "/api/receiver", endpoint_port: 5500, isApple: true)

I just sent another binary now instead of try: if config.isVirtualMachine { print("config.isVirtualMachine") } which crash here - this line is an attempt to the client help me to debug, i create a new global var configisVirtualMachine: Bool = false and I am setting its value and gonna try to use it instead of the config property value.. I am getting out of options here :(

As you are looking for any option, may try this one (may be that will change something in Terminal ?).

Move

var config = Config() // global config var
var Instrumentation: STInstrumentation // global Instrumentation var

before

func getInstrumentation() -> STInstrumentation {
Newbie and getting a error with struct data access
 
 
Q