Overview
Features
Download
Documentation
Community
Add-Ons & Services

Plugin question

Please post support and help requests here.

Plugin question

Postby bird » 12 Mar 2008, 09:01

I have an application that has a Singleton that has a NotificationCenter
^
Code: Select all


class  Messenger
{
public:
   void addObserver(const Poco::AbstractObserver& observer)
   {   
      nc.addObserver(observer);
   }
   void SendMessage()
   {   
      nc.postNotification(new Notification);
   }
private:
   NotificationCenter nc;
};

^
And my plugin registers as an Observer on startup
^
Code: Select all

class View : public SimplePlugin
{

public:
   void Init( Messenger* inst)
   {   
      inst->addObserver(Poco::NObserver
               (*this,&View::HandleMessage));
   }
   void HandleMessage( const Poco::AutoPtr& note )
   {   
   }
};

^
And here's the application code
^
Code: Select all

int main(int argc, char** argv)
{
   std::string path = "../Plugins/SimplePlugin";
   path.append(SharedLibrary::suffix());

   ClassLoader cl;
   cl.loadLibrary(path);
   SimplePlugin* plugin = cl.classFor("View").create();

   {
      SingletonHolder messenger;
      plugin->Init( messenger.get() );
      messenger.get()->SendMessage();

      Thread::sleep(100);   
   }

   delete plugin;
   cl.unloadLibrary(path);
   return 0;
}

^
The plugin does get the Notification that's sent but the when the Messenger goes out of scope there's a crash in the NotificationCenter destructor as it deletes the ObserverList.
Can someone explain why that is happening?
I'm a bit of a newbie swimming over his head so I expect it some ignorance on my part but I don't understand what I'm doing wrong.

Thanks!
-Steve
bird
 
Posts: 19
Joined: 22 Sep 2007, 16:57

Re: Plugin question

Postby guenter » 12 Mar 2008, 18:13

The Observer template code for your plugin has been instantiated by the compiler in your plugin shared library. When the NotificationCenter removes all its observers, the destructor of every observer instantiation is also called. Unfortunately, at this time, your plugin library containing the destructor code for the observer has already been unloaded, so there's no code for the destructor to call, resulting in the crash. You could do a removeObserver() in your plugin's destructor to avoid this problem.


guenter
 
Posts: 1129
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Plugin question

Postby bird » 13 Mar 2008, 03:41

Thanks for the reply. Sorry, it's a bit confusing because for some reason template parameters are being stripped from my example code but the Messenger object is held by the SingletonHolder and therefore is destroyed when the holder leaves it's private scope. So in this case the NotificationCenter is destroyed before the plugin is deleted and the library is unloaded.

I tried a removeObserver() in the plugin destructor and deleted the plugin before the NotificationCenter is detroyed and now a crash occurs in NotificationCenter::removeObserver on the ScopedLock construction. I believe the _mutex is invalid for reasons I don't understand. :~)

-Steve

bird
 
Posts: 19
Joined: 22 Sep 2007, 16:57

Re: Plugin question

Postby bird » 13 Mar 2008, 05:17

I tried a removeObserver() in the plugin destructor and deleted the plugin before the NotificationCenter? is detroyed and now a crash occurs in NotificationCenter::removeObserver on the ScopedLock? construction. I believe the _mutex is invalid for reasons I don't understand.
---------------

Sorry, that was just due to pilot error. Now the plugin destructor removes the Observer from the NotificationCenter and hasObservers() returns false.

But there's still crashing in the NotificationCenter destructor.

-Steve
bird
 
Posts: 19
Joined: 22 Sep 2007, 16:57


Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests