当数据为 NULL 时,访问链表的数据元素时应用程序崩溃

Application crash while accessing data element of linked list when data is NULL

提问人:iosdev1111 提问时间:5/23/2019 最后编辑:iosdev1111 更新时间:7/11/2019 访问量:109

问:

我正在使用以下代码遍历目标 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,那么我将无法访问它,并且应用程序本身正在崩溃。如何防止这种情况?我附上了应用程序崩溃的位置以及该列表当时包含的值的屏幕截图。enter image description here

ios objective-c 链接列表 null

评论

0赞 tww0003 5/23/2019
在 while 循环中,而不是尝试 .linphone (github.com/guardianproject/linphone-ios-secure/blob/master/...) 的旧 github 存储库具有确保以这种方式存在的示例代码。我对 objective-c 中结构的所有来龙去脉都不是很熟悉(我很少使用它们),但我相信它们被初始化为 0,所以如果它们中没有任何东西应该返回 true。如果我错了,请有人纠正我。如果有效,我会发布此评论作为答案,否则我认为它应该留在这里while (calls)while (calls != 0)callssomeStruct == 0
0赞 iosdev1111 5/24/2019
我尝试过 while(调用 != 0),它仍然不起作用。

答:

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;
                    }
                }