Page 1 of 1

Interface IUnknown Release Object

Posted: Sun Jun 02, 2019 10:43 am
by mk-soft
Microsoft wrote
Remarks
When the reference count on an object reaches zero, Release must cause the interface pointer to free itself. When the released pointer is the only existing reference to an object (whether the object supports single or multiple interfaces), the implementation must free the object.
Note that aggregation of objects restricts the ability to recover interface pointers.
With the objects ActiveScript or ActiveScriptParse the objects are already released with Ref-Counter 1 and a call from release to zero leads to a memory access error.

Does MS not take its own doku seriously, or do I interpret something wrong here?

Re: Interface IUnknown Release Object

Posted: Sun Jun 02, 2019 11:28 am
by Mijikai
Found this on stackoverflow:
1) The examples you refer to have to be getting their COM interface pointers from somewhere.
This would be by making COM calls, e.g., CoCreateInstance() and QueryInterface().
These calls are passed the address of your raw pointer and set that raw pointer to the appropriate value.
If they weren't also implicitly AddRef'ed, the reference count might be 0 and COM could delete the associated COM object
before your program could do anything about it. So such COM calls must include an implicit AddRef() on your behalf.
You are responsible for a Release() to match this implicit AddRef() that you instigated with one of these other calls.
https://stackoverflow.com/questions/393 ... -questions

Re: Interface IUnknown Release Object

Posted: Sun Jun 02, 2019 11:47 am
by mk-soft
I checked again...

CoCreateInstance of ActiveScript starts with Ref-Counter 1 to probably prevent an automatic delete.
So also with Ref-Counter 1 the object is released again.

So all is well :wink: