[英]Objective-C Result from a Static Method saved to class instance variable giving “EXC_BAD_ACCESS” when used
I am trying to store the md5 string as a class instance variable instead of the actual password. 我试图将md5字符串存储为类实例变量而不是实际密码。 I have a static function that will return a md5 string which I'm trying to store in an instance variable instead of the actual password. 我有一个静态函数,它将返回一个md5字符串,我试图将其存储在实例变量而不是实际密码中。
I have the following setter for my class instance variable: 我的类实例变量有以下setter:
-(void)setPassword:(NSString *)newpass{
if(newpass != password){
password = [utils md5HexDigest:newpass];
}
}
This will pass back the correct md5 string and save it to the password variable in my init function: [self setPassword:pword];. 这将传回正确的md5字符串并将其保存到我的init函数中的密码变量:[self setPassword:pword] ;.
If I call another instance method and try to access self.password" I will get "EXC_BAD_ACCESS". 如果我调用另一个实例方法并尝试访问self.password“我会得到”EXC_BAD_ACCESS“。
I understand that the memory is getting released, but I have no clue to make sure it stays. 我知道内存正在被释放,但我不知道确保它保持不变。
I have tried alloc init with autorelease with no luck. 我已尝试使用自动释放的alloc init而没有运气。
This is the md5HexDigest function getting called during the init (graciously found in another stackoverflow question): 这是在init期间调用的md5HexDigest函数(在另一个stackoverflow问题中很容易找到):
+ (NSString*)md5HexDigest:(NSString*)input {
const char* str = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++)
[ret appendFormat:@"%02x",result[i]];
return ret;
}
Any help/pointers would be greatly appreciated. 任何帮助/指针将不胜感激。 I would rather have the md5 string saved in memory than the actual password calling the md5 every time I needed to use the password. 我宁愿在内存中保存md5字符串,而不是每次需要使用密码时调用md5的实际密码。
Thanks in advance. 提前致谢。
Both David and pwc are correct, but missing an important detail. David和pwc都是正确的,但遗漏了一个重要的细节。
setPassword:
should copy
the inbound parameter. setPassword:
应该copy
入站参数。 This will both ensure that 这将确保这一点
retain
ed properly as the other answers imply and also that 它正确地retain
,因为其他答案暗示也是如此 ie something like: 即:像:
-(void)setPassword:(NSString *)newpass{
[password release];
password = [[utils md5HexDigest: newpass] copy];
}
Your setter needs to retain
the new value (the one returned from [utils md5HexDigest:newpass]
). 你的setter需要retain
新值(从[utils md5HexDigest:newpass]
返回的值)。 (and don't forget to release
the old value too...) (并且不要忘记release
旧值......)
Also, the test if(newpass != password)
does not make any sense: newpass
is an actual password, but password
is the md5 digest. 此外,测试if(newpass != password)
没有任何意义: newpass
是实际密码,但password
是md5摘要。
The NSString returned by md5HexDigest
is autoreleased (because NSMutableString stringWithCapacity
returns an autoreleased string). md5HexDigest
返回的NSString是自动释放的(因为NSMutableString stringWithCapacity
返回一个自动释放的字符串)。 You need to retain it in setPassword
. 您需要将其保留在setPassword
。 Otherwise, it is being freed by the autorelease pool. 否则,它将被自动释放池释放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.