Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

We are looking for a way to clean up resources allocated inside an IThread where the resource pointer resides on the stack within the IThread. We found the ITHREAD_HoldRsc() method that "associates a generic IBase pointer with the lifetime of an IThread". We are guessing that the Ithread will automatically call the IBase_Release() method on all such pointers when ITHREAD_Release() is called (ITHREAD_Release() is actually undocumented in the Brew API Reference!?!). Qusetion 1: Can you confirm that this is the intended usage of the ITHREAD_HoldRsc() method? We tried to Google for information about ITHREAD_HoldRsc() and only found one larger example here: http://www.devx.com/wireless/Article/32077/1954

In this example the author uses ITHREAD_HoldRsc() for a resource that is allocated outside the IThread stack. When the application exits, the author then releases that resource explicitly AND implicitly (we believe) by releasing the IThread object. Wouldn't releasing an already free object lead to unstable code? Question 2: Can you confirm that this author has misunderstood the purpose of ITHREAD_HoldRsc() in the example code?

The object itself isn't allocated from the stack in these cases, but the only reference to the object is on the stack. This would normally be cleaned up when the thread returned through that function, but it's possible to cancel an IThread while its suspended (long-jumping past the normal cleanup). This use case is exactly what IThread_HoldRsc() is designed for. Regarding example code:- The code appears to be using HoldRsc() correctly (I can only assume what I don't see), but the example is contrived. They could have put the IGraphics instance on the thread's stack frame instead of in the thread context, making it fit the intended use of HoldRsc() better. The way they've constructed it, no reference has been leaked, but they decided not to use the SThreadCtx destructor to clean up the IGraphics instance.

The object itself isn't allocated from the stack in these cases, but the only reference to the object is on the stack. This would normally be cleaned up when the thread returned through that function, but it's possible to cancel an IThread while its suspended (long-jumping past the normal cleanup). This use case is exactly what IThread_HoldRsc() is designed for. Regarding example code:- The code appears to be using HoldRsc() correctly (I can only assume what I don't see), but the example is contrived. They could have put the IGraphics instance on the thread's stack frame instead of in the thread context, making it fit the intended use of HoldRsc() better. The way they've constructed it, no reference has been leaked, but they decided not to use the SThreadCtx destructor to clean up the IGraphics instance.