On Jul 17, 2006, at 7:37 PM, Phil M wrote:
When you lock an object, does it *only* mean that the plugin is
requesting REALbasic to increment the reference counting for that
object? Or does it mean that the object is locked for further
editing by other methods/controls while the Plugin retains the lock?
LockObject or LockString increases the refcount. Any object or string
that has a refcount > 0 will not be destroyed.
Also I assume that if you lock a local variable without an unlock
that this is potentially a memory leak?
Is there any runtime method that you can do to detect a missing
unlock?
Yes, you would want to call the Runtime class method, using dynamic
access (REALLoadObjectMethod) from within the plugin, or call a
runtime method from RB.
What happens if you accidently unlock an object which was already
unlocked?
Depends, if an object has a refcount of 3 then unlocking it twice
wouldn't have an immediate effect, but could potentially lead later
to premature removal of the object causing great havoc, memory
corruption and a crash. THis is particular so if you muck with an
object instance which the user owns, not the plugin. You can however
call REALLockObject or REALUnlockObject multiple times if the
REALobject references a nil object.
Testing a refcount very quickly is done by:
void DEBUGOBJECT(REALobject object)
{
long countRef = 0;
REALstring str = nil;
if (object)
{
countRef = *(long*) (((char*)object) + 12);
XControl xc = object;
str = xc.name();
}
else
str = REALBuildString("nil", strlen("nil"));
dlog("Name = %s, ID->(%x), Refs->(%x)", str->CString(), object,
countRef);
REALUnlockString(str);
}
where XControl means a C++ class that uses dynamic access wrapping
the Control class of REALbasic. It then is easy to get the name of
the control. Note that "countRef = *(long*) (((char*)object) + 12);"
is a hack to get at the refcount quickly, should not be used to
manipulate an object and can break any moment. Earlier versions of RB
had a different object struct, and the offset 12 was then 0.
If you are going to play with arrays, consider:
void DEBUGARRAY(REALobjectArray array)
{
long countRef = *(long*) (((char*)array) + 0);
dlog("Array: Refcount->(%x), Address->(%x)", countRef, array);
}
but again, the same considerations hold.
Go figure the string refcount.....
Alfred
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>