Please RELEASE Me

Did you know if you add an object to a collection, and the object contains a RELEASE method, it will automatically be removed from the collection when the object is released?

Check out this example code:

oCollection = CREATEOBJECT("MyCollection")
FOR i = 1 TO 10
   IF MOD(i,3) == 0
      ?"No Release"
      o = CREATEOBJECT("ObjNoRelease")
      oCollection.Add(o)
   ELSE  
      o = CREATEOBJECT("ObjWithRelease")
      oCollection.Add(o)
      ?"With Release()"
   ENDIF
ENDFOR
?"Collection Count: " + ;
  ALLTRIM(TRANSFORM(oCollection.Count))
lnCount = oCollection.Count
FOR i = lnCount TO 1 STEP -1
   oObj = oCollection.Item(i)
   TRY
      oObj.Release()
   CATCH
   FINALLY
      ?"Collection Count: " + ;
      ALLTRIM(TRANSFORM(oCollection.Count))
   ENDTRY
ENDFOR
?"Collection Count: " + ;
   ALLTRIM(TRANSFORM(oCollection.Count))
DEFINE CLASS ObjWithRelease As Session
   PROCEDURE Release
      RELEASE THIS
   ENDPROC
ENDDEFINE
DEFINE CLASS ObjNoRelease As Session
ENDDEFINE

DEFINE CLASS MyCollection As Collection
ENDDEFINE

Objects with a RELEASE method magically disappear from the collection when they are released. This makes for an easy way to keep track of opened threads when using Christof’s DMULT.DLL for multithreading. All I have to do is make sure my callback objects have a Release method, and periodically check the “ThreadManager” collection Count property to see how many threads may still be running.

One Comment

Comments are closed.