简体   繁体   English

如何在取消引用之前检查迭代器(C ++)?

[英]how to check iterator before dereferencing (C++)?

I am using static analysis tool to find a bugs in my code.It shows the error before dereferencing u have to check your iterator is NULL or not.How can I check this? 我正在使用静态分析工具在我的代码中查找错误。它在取消引用之前必须显示错误,您必须检查迭代器是否为NULL。如何检查此错误? See the code below: 请参见下面的代码:

for(ClientThreadGroupList_t::iterator it = m_vecClientThreadGroup.begin(); it != m_vecClientThreadGroup.end(); it++) 
{
  nCount += (*it)->ConsoleList(pSocket); //error
}

The check for iterator validity is already in your code: 代码中已经存在对迭代器有效性的检查:

it != m_vecClientThreadGroup.end()

Note that, in general, you can't check whether an iterator is valid or dereferencably; 请注意,通常来说,您无法检查迭代器是否有效或是否可引用。 but, when you're iterating over a sequence, you know that the iterators will be dereferencable until you reach the end of the sequence. 但是,当您遍历序列时,您知道迭代器将是可取消引用的,直到到达序列末尾为止。

Since your sequence apparently contains pointers, you might also need to check whether they are null before dereferencing: 由于您的序列显然包含指针,因此您可能还需要在取消引用之前检查它们是否为空:

if (*it) {
    nCount += (*it)->ConsoleList(pSocket);
}

Note that this will only check for null pointers; 注意,这只会检查空指针。 you can't in general check whether or not a non-null pointer actually points to a valid object. 您通常无法检查非null指针是否实际上指向有效对象。

for(ClientThreadGroupList_t::iterator it = m_vecClientThreadGroup.begin(); it != m_vecClientThreadGroup.end(); it++) {
if (*it != 0)
   nCount += (*it)->ConsoleList(pSocket); //error    
}

m_vecClientThreadGroup.begin() returns NULL ptr as i suppose 我想m_vecClientThreadGroup.begin()返回NULL ptr

and are you sure you want to use (*it)->ConsoleList(pSocket) instead of it->ConsoleList(pSocket)? 并且确定要使用(* it)-> ConsoleList(pSocket)而不是它-> ConsoleList(pSocket)吗?

A pointer can implicitly converted to Boolean type , simply call if(*it) to test nullity of a pointer 指针可以隐式转换为布尔类型 ,只需调用if(*it)即可测试指针if(*it)为空

for(ClientThreadGroupList_t::iterator it = m_vecClientThreadGroup.begin(); 
    it != m_vecClientThreadGroup.end(); ++it) 
//                                      ^^^^ // better practice
{
    if (*it)  // test if a pointer is null or not
    {
        nCount += (*it)->ConsoleList(pSocket);  
    }
}

Update from comment: 评论更新:

Looks like the issue is multiple threading issue, you need to provide lock mechanism to avoid another thread invalid your current iterators. 看起来问题是多线程问题,您需要提供锁定机制,以避免另一个线程使您的当前迭代器无效。

You can simply check it with an if condition : 您可以简单地使用if条件进行检查:

for(ClientThreadGroupList_t::iterator it = m_vecClientThreadGroup.begin(); it != m_vecClientThreadGroup.end(); it++) 
{
    if ( (*it) != NULL ) // or in C++11 != nullptr
        nCount += (*it)->ConsoleList(pSocket); //error
}

It is the call to ConsoleList method on a NULL object who cause the error I suppose. 这是导致NULL错误的对NULL对象上的ConsoleList方法的调用。 Because, if there is no element in your list , begin() will be the same as end() and you will never enter in the loop. 因为,如果list没有元素,则begin()将与end()相同,并且您将永远不会进入循环。

For brevity you can do : 为简便起见,您可以执行以下操作:

if ( *it ) // Implicit convertion here...

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

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