Skip to main content
Inspiring
May 26, 2014
Answered

Attaching and Detaching Observers to a document, what is the correct way?

  • May 26, 2014
  • 1 reply
  • 489 views

Hi,

I came across below code snippet in many of the sample PLugins where Observer is being:

  • Attached on the kAfterOpenDocSignalResponderService
  • Detached on the on kBeforeCloseDocSignalResponderService

Same DocRef is being used in both the cases but the Observer being referenced is new each time, so is it a correct way to do it? and Will it work?

GTTxtEdtResponder::attachDetachStoryObserver(UIDRef storyUIDRef, bool16 bAttach)

{

  ErrorCode status = kFailure;

  do{

  // while we observe all stories (whether they are accessible or not), we only let accessible

  // stories affect the cache. See the observer implementation for this logic.

  InterfacePtr<IObserver> iGTTxtEdtObserver(storyUIDRef,IID_IGTTXTEDTSTORYOBSERVER);           //<--new reference created

  if (iGTTxtEdtObserver == nil){

  ASSERT_FAIL("GTTxtEdtResponder::attachDetachStoryObserver - no observer on story?");

  break;

  }

  bAttach ? iGTTxtEdtObserver->AutoAttach() :iGTTxtEdtObserver->AutoDetach();

  status = kSuccess;

  }while (kFalse);

  return status;

}

If you look closely, you can see that every time a detach is being called after attach, a new Observer reference is created.

Is correct way to do it?

This topic has been closed for replies.
Correct answer Markus Freitag

Hello kapoor_aman27,

I can't see any mistake. Attach is called when the responder receives the kAfterOpenDocSignalResponderService signal for each observer which was created for the just opend document. Detach is called when the responder receives the kBeforeCloseDocSignalResopnderService Signal for each observer of the document before it will be closed and the observer will be destroyed.

Markus

1 reply

Markus FreitagCorrect answer
Inspiring
May 26, 2014

Hello kapoor_aman27,

I can't see any mistake. Attach is called when the responder receives the kAfterOpenDocSignalResponderService signal for each observer which was created for the just opend document. Detach is called when the responder receives the kBeforeCloseDocSignalResopnderService Signal for each observer of the document before it will be closed and the observer will be destroyed.

Markus

Inspiring
May 26, 2014

yes this is working fine.

But my question here is that  we creating a new reference of IObserver at the time of Detach and then calling the autoDetach method.

So what about the reference we created at the time of Attach? would'nt it be left like a dangling pointer pointing no where?

Legend
May 26, 2014

Please revisit the documentation for such architecture fundamentals. InDesign uses reference counters to manage boss objects. Have a look at the last few lines of InterfacePtr.h - while that specific constructor worked as AddRef(), the reference is released by the destructor of InterfacePtr. If the reference counter reaches 0, the whole boss is purged.

Dirk