| 605 | } |
| 606 | |
| 607 | int asCScriptObject::Release() const |
| 608 | { |
| 609 | // Clear the flag set by the GC |
| 610 | gcFlag = false; |
| 611 | |
| 612 | // If the weak ref flag exists it is because someone held a weak ref |
| 613 | // and that someone may add a reference to the object at any time. It |
| 614 | // is ok to check the existance of the weakRefFlag without locking here |
| 615 | // because if the refCount is 1 then no other thread is currently |
| 616 | // creating the weakRefFlag. |
| 617 | if( refCount.get() == 1 && extra && extra->weakRefFlag ) |
| 618 | { |
| 619 | // Set the flag to tell others that the object is no longer alive |
| 620 | // We must do this before decreasing the refCount to 0 so we don't |
| 621 | // end up with a race condition between this thread attempting to |
| 622 | // destroy the object and the other that temporary added a strong |
| 623 | // ref from the weak ref. |
| 624 | extra->weakRefFlag->Set(true); |
| 625 | } |
| 626 | |
| 627 | // Call the script destructor behaviour if the reference counter is 1. |
| 628 | if( refCount.get() == 1 && !isDestructCalled ) |
| 629 | { |
| 630 | // This cast is OK since we are the last reference |
| 631 | const_cast<asCScriptObject*>(this)->CallDestructor(); |
| 632 | } |
| 633 | |
| 634 | // Now do the actual releasing |
| 635 | int r = refCount.atomicDec(); |
| 636 | if( r == 0 ) |
| 637 | { |
| 638 | // Flag this object as being destroyed so the application |
| 639 | // can be warned if the code attempts to resurrect the object |
| 640 | // during the destructor. This also avoids a recursive call |
| 641 | // to the destructor which would crash the application if it |
| 642 | // really does resurrect the object. |
| 643 | if( !hasRefCountReachedZero ) |
| 644 | { |
| 645 | hasRefCountReachedZero = true; |
| 646 | |
| 647 | // This cast is OK since we are the last reference |
| 648 | const_cast<asCScriptObject*>(this)->Destruct(); |
| 649 | } |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | return r; |
| 654 | } |
| 655 | |
| 656 | void asCScriptObject::CallDestructor() |
| 657 | { |
no test coverage detected