Page 1 of 1

self-assignment bug in hsTArray

PostPosted: Thu Feb 18, 2010 4:41 pm
by Branan
The following code:
Code: Select all
hsTArray a;
a = a
will not work as expected. 'a' clears its data structures, then attempts to copy from itself, copying nothing. The correct method is to check for self-assignment. Patch:
Code: Select all
Index: core/Util/hsTArray.hpp
===================================================================
--- core/Util/hsTArray.hpp      (revision 359)
+++ core/Util/hsTArray.hpp      (working copy)
@@ -26,6 +26,8 @@
     }

     hsTArray<T>& operator=(const hsTArray& cpy) {
+        if (&cpy == this) return *this;
+
         clear();
         count = cpy.count;
         data = new T[count];

Re: self-assignment bug in hsTArray

PostPosted: Thu Feb 18, 2010 6:40 pm
by Zrax
Hmm, dunno why anyone would do that, but thanks anyway for catching it ;)

EDIT: Applied fix to hsTList as well