提问人:iosdev1111 提问时间:5/23/2019 最后编辑:iosdev1111 更新时间:7/11/2019 访问量:109
当数据为 NULL 时,访问链表的数据元素时应用程序崩溃
Application crash while accessing data element of linked list when data is NULL
问:
我正在使用以下代码遍历目标 c 中的链表
const MSList *calls = linphone_core_get_calls(LC);
if (calls == NULL)
{
[self dismissCtrl];
//how to check current is which screen is on
// while ((currentView == CallView.compositeViewDescription) ||
// (currentView == CallIncomingView.compositeViewDescription) ||
// (currentView == CallOutgoingView.compositeViewDescription)) {
// [self popCurrentView];
// }
} else {
linphone_call_resume((LinphoneCall *)calls->data);
while (calls)
{
if(calls->data != NULL && calls->data != nil && calls->data != (__bridge void *)((id)[NSNull null]))
{
//crash
if (linphone_call_get_state((LinphoneCall *)calls->data) == LinphoneCallIncomingReceived ||
linphone_call_get_state((LinphoneCall *)calls->data) == LinphoneCallIncomingEarlyMedia) {
[self displayIncomingCall:(LinphoneCall *)calls->data];
break;
}
}
calls = calls->next;
}
当整个列表不为 null 但其数据、上一个或下一个值为 NULL 时,应用程序崩溃。我添加了代码来检查数据是否为 NULL,但如果它(数据)为 NULL,那么我将无法访问它,并且应用程序本身正在崩溃。如何防止这种情况?我附上了应用程序崩溃的位置以及该列表当时包含的值的屏幕截图。
答:
0赞
iosdev1111
7/11/2019
#1
if (calls == NULL) {
[self dismissCtrl];
//how to check current is which screen is on
// while ((currentView == CallView.compositeViewDescription) ||
// (currentView == CallIncomingView.compositeViewDescription) ||
// (currentView == CallOutgoingView.compositeViewDescription)) {
// [self popCurrentView];
// }
}
else
{
size_t count = bctbx_list_size(calls);
linphone_call_resume((LinphoneCall *)calls->data);
int i = 0;
while (calls )
{
if(i < count)
{
if ( calls->data == (__bridge void *)((id)[NSNull null]) || calls->data == NULL || calls->data == nil )
{
return;
}
LinphoneCall *objCall = (LinphoneCall *)calls->data;
LinphoneCallState state = (objCall != NULL) ? linphone_call_get_state(call) : 0;
if (state == LinphoneCallIncomingReceived ||
state == LinphoneCallIncomingEarlyMedia)
{
[self displayIncomingCall:(LinphoneCall *)calls->data];
break;
}
//calls ? calls->data : NULL
// calls = calls ? calls->next : NULL;
calls = calls->next;
i = i+1;
}
else
{
break;
}
}
评论
while (calls)
while (calls != 0)
calls
someStruct == 0