How bad is “if (!this)” in a C++ member function?

前端 未结 11 667
不思量自难忘°
不思量自难忘° 2021-02-04 23:16

If I come across old code that does if (!this) return; in an app, how severe a risk is this? Is it a dangerous ticking time bomb that requires an immediate app-wide

11条回答
  •  臣服心动
    2021-02-04 23:50

    If you have many GetLookup functions return NULL, then you're better off fixing code that calls methods using a NULL pointer. First, replace

    if (!this) return NULL;
    

    with

    if (!this) {
      // TODO(Crashworks): Replace this case with an assertion on July, 2012, once all callers are fixed.
      printf("Please mail the following stack trace to myemailaddress. Thanks!");
      print_stacktrace();
      return NULL;
    }
    

    Now, carry on with your other work, but fix these as they roll in. Replace:

    GetLookup(x)->Lookup(y)...
    

    with

    convert_to_proxy(GetLookup(x))->Lookup(y)...
    

    Where conver_to_proxy does returns the pointer unchanged, unless it's NULL, in which case it returns a FailedLookupObject as in my other answer.

提交回复
热议问题