NSString 内存错误/崩溃

发布于 2024-12-23 07:30:52 字数 742 浏览 2 评论 0原文

我有用户名和密码的 UITextField,一旦用户点击提交按钮,我就会执行

 NSString* user_name;
 NSString* pass_word;
 UITextField * username;
 UITextField * password;
 NSString * url;

 user_name=username.text;
 pass_word=password.text;


  url = [[NSString stringWithFormat:@"devices.json?user=%@&pswd=%@", user_name,pass_word];

但是,当我尝试这样做时,有时我会遇到 url 的 EXEC_BAD_ACCESS 问题,我使用 Zombie 工具并解决了它 %@ 是导致 url 字符串问题的原因。 %@ 期待一个包含 user_name、pass_word 的 NSString,但不知何故,在这个过程中,user_name 和 pass_word 发生了变异,并得到了一些垃圾值,而不是 NSString。

我尝试使用 [用户名保留]; [密码保留]; [网址保留];但没有帮助。 url 的 %@ 参数始终存在 EXEC_BAD_ACCESS 问题。

其次,我能够输出用户名、密码,有时会得到一些没有意义的值。我不知道他们从哪里得到这些价值观。我没有输入这些值。我正在进行 http 调用。不知何故,http 返回值潜入了 pass_word 和 user_name 中,我不知道为什么要这样做。

I have UITextField of username and password, once the user hit the submit button I do

 NSString* user_name;
 NSString* pass_word;
 UITextField * username;
 UITextField * password;
 NSString * url;

 user_name=username.text;
 pass_word=password.text;


  url = [[NSString stringWithFormat:@"devices.json?user=%@&pswd=%@", user_name,pass_word];

However, when I try that, sometimes I got EXEC_BAD_ACCESS issue with url, I used the Zombie tool and nailed it %@ is the one causing issue with url string. %@ expecting a NSString of user_name, pass_word but somehow in the process, the user_name and pass_word got mutated and got some junk values, not NSString anymore.

I try to use [user_name retain]; [pass_word retain]; [url retain]; but didn't help. It keep having EXEC_BAD_ACCESS issue with %@ parameter of the url.

secondly I able to output the user_name, pass_word, it sometimes got some values that doesn't make sense. I don't where they got those values. I didn't put those values in. I am making a http call. somehow the http returned values sneaked inside the pass_word, and user_name, I have no idea why it's doing that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝對不後悔。 2024-12-30 07:30:52

其次,我能够输出用户名、密码,有时会得到一些没有意义的值。

这通常意味着您分配未初始化的变量...并且它解释了如果您尝试记录这些值时发生的崩溃。但不可能从答案中包含的代码部分找出问题所在。

尝试记录用户名和密码的内容。 (uitextfields)..您应该使用更好的变量名称。

secondly I able to output the user_name, pass_word, it sometimes got some values that doesn't make sense.

this usually means that you assign not initialized variables... and it explains the crashes if you try to nslog the values. but it's not possible to find out what's wrong from the part of the code you have included in your answer.

try to log the contents of username and password. (the uitextfields) .. you should use better variable names.

东京女 2024-12-30 07:30:52

来自评论:

我在common.h文件中声明了NSString *user_name, *pass_word,其中有很多
文件需要访问该文件。

除非你有一个单例类,否则每次你分配/初始化一个新的 Common 对象时,它都会有垃圾值。 Objective-C 最佳实践建议不要采用这种设计。对于大多数数据,可以存储在 plist 或 NSUserDefaults 中,但对于用户名和密码字符串,您应该将其存储在钥匙串中。更好的是,如果服务器接受的话,您应该将密码存储为哈希值。

然后在 NSString 上使用一个类别(类似于 NSString+Authentication),该类别具有从钥匙串中提取数据并将其插入钥匙串的方法。

From a comment:

I declare NSString *user_name, *pass_word in common.h file which many
files need to access that one.

Unless you've got a singleton class, every time you alloc/init a new Common object it's going to have trash values. Objective-C best practices recommend against this sort of design. For most data, it's fine to store in a plist or in NSUserDefaults, but for the username and password string, you should store it in the keychain. Even better, you should store your password as a hash if the server will accept it.

Then use a category on NSString (call it something like NSString+Authentication) that has methods to pull the data from the keychain as well as insert it into the keychain.

绮烟 2024-12-30 07:30:52

也尝试使用分配,看看是否有帮助:

url = [[NSString alloc] initWithFormat:@"devices.json?user=%@&pswd=%@", user_name,pass_word];

Try using the allocation as well and see if it helps:

url = [[NSString alloc] initWithFormat:@"devices.json?user=%@&pswd=%@", user_name,pass_word];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文