Author Topic: about BetterList.Contains  (Read 2213 times)

raven

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
about BetterList.Contains
« on: February 18, 2014, 03:12:24 AM »
We are on NGUI2.6.1, to me the following code is problematic:
        public bool Contains (T item)
   {
      if (buffer == null) return false;
      for (int i = 0; i < size; ++i) if (buffer.Equals(item)) return true;
      return false;
   }

It assumes that buffer is not null but what if buffer is destroyed outside?
In this case, we will want to figure out why we destroy materials when there is reference to them. But at the same time, we don't want it crash the app for release build.

Anyone has comments on this?

Best,

Raven

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: about BetterList.Contains
« Reply #1 on: February 18, 2014, 10:13:32 AM »
NGUi 2.6.1 is extremely old and is no longer supported. Also your question doesn't make sense to me. What assumes that buffer is not null? There is a null check  on the very first line.

raven

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: about BetterList.Contains
« Reply #2 on: February 23, 2014, 12:49:17 AM »
I checked the latest version, the code is exactly the same.

It might because the format check of the forum removing some characters. the first check is for the buffer itself.
The problem here is that i'th item in the buffer isn't null checked.

Best,

Raven

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: about BetterList.Contains
« Reply #3 on: February 23, 2014, 01:25:54 AM »
This is what it is:
  1. public bool Contains (T item)
  2.         {
  3.                 if (buffer == null) return false;
  4.                 for (int i = 0; i < size; ++i) if (buffer[i].Equals(item)) return true;
  5.                 return false;
  6.         }

and this is what you want, yes?

  1. public bool Contains (T item)
  2.         {
  3.                 if (buffer == null) return false;
  4.                 for (int i = 0; i < size; ++i) if (buffer[i] != null && buffer[i].Equals(item)) return true;
  5.                 return false;
  6.         }

raven

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: about BetterList.Contains
« Reply #4 on: February 23, 2014, 05:01:54 AM »
Thanks Nicki, yes basically I want the != check you added but I also want to know the reason why NGUI choose not adding it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: about BetterList.Contains
« Reply #5 on: February 23, 2014, 12:31:01 PM »
Because this makes an assumption that "T" is a nullable type.

What if you have a BetterList<int> ? That's not a nullable type and the code will no longer compile.