简体   繁体   English

初始化结构时EXC_BAD_ACCESS

[英]EXC_BAD_ACCESS when init a struct

I'm developing an app in Objective-C/OSX (first try). 我正在使用Objective-C / OSX开发应用程序(第一次尝试)。 I had a struct (NSDevice custom type) which need to be accessed as a public/extern variable to allow the different interface of the class to access it. 我有一个结构(NSDevice自定义类型),需要将其作为公共/外部变量进行访问,以允许类的不同接口对其进行访问。

Here is the declaration in the header. 这是标题中的声明。

struct NSDevice{
    LIBMTP_raw_device_t * usbrawdevice;
    int numusbrawdevice;
    uint32_t rawdeviceID;

    LIBMTP_mtpdevice_t *device;
};

extern struct NSDevice *Device;

@interface DeviceManager : NSObject

- (void) openDevice;
- (void) closeDevice;

@end

and how I implement it in the source file 以及我如何在源文件中实现它

#import "DeviceManager.h"

struct NSDevice *Device = NULL;


@implementation DeviceManager



- (id)init{
    self = [super init];
    if(self){
        NSLog(@"Init");
        LIBMTP_Init();

        Device->device = NULL;
        Device->numusbrawdevice = 0;
        Device->rawdeviceID = 0;
        Device->usbrawdevice = NULL;
    }
    return self;
}

- (NSMtp_error) openDevice {
    LIBMTP_error_number_t error = LIBMTP_ERROR_GENERAL;

    NSLog(@"Opening Device");

    error = LIBMTP_Detect_Raw_Devices(&Device->usbrawdevice, &Device->numusbrawdevice);

When trying to init the struct in the init interface, I got an EXC_BAD_ACCESS. 当尝试在init接口中初始化该结构时,我得到了EXC_BAD_ACCESS。 Any idea ? 任何想法 ?

Can I use the "struct" or is there a better way in Objective-C ? 我可以使用“ struct”还是在Objective-C中有更好的方法?

Thx 谢谢

Seb, you need to allocate the space for Device . Seb,您需要为Device分配空间。

Try doing this: 尝试这样做:

- (id)init{
    self = [super init];
    if(self){
        NSLog(@"Init");
        LIBMTP_Init();

        Device = malloc(sizeof(NSDevice));
        Device->device = NULL;
        Device->numusbrawdevice = 0;
        Device->rawdeviceID = 0;
        Device->usbrawdevice = NULL;
    }
    return self;
}

and declare your structure like this: 并这样声明你的结构:

typedef struct {
    LIBMTP_raw_device_t * usbrawdevice;
    int numusbrawdevice;
    uint32_t rawdeviceID;

    LIBMTP_mtpdevice_t *device;
} NSDevice;

Also, I highly recommend changing the name of NSDevice to SebDevice or something that doesn't start with NS as those prefix characters usually signify something built into the MacOS SDK and it's going to confuse anyone else who has to look at your code after you depart the project. 另外,我强烈建议NSDevice的名称NSDeviceSebDevice或不以NS开头的名称,因为这些前缀字符通常表示MacOS SDK中内置的内容,这会使其他人在离开后必须查看您的代码感到困惑该项目。

And one last thing, global variables like this shouldn't start with capital letters. 最后一件事,像这样的全局变量不应以大写字母开头。 In Objective-C, best practice is for variables to start with lower case letters or maybe a g (for global) or an underscore. 在Objective-C中,最佳做法是使变量以小写字母开头,或者以g (表示全局)或下划线开头。 Change Device to gDevice . Device更改为gDevice

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM