Sign in to follow this  

Plugin system design

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm in the process of rewriting bits and peices of my project code in an attempt to streamline the classes and managers and whatnot. I'm thinking about reworking some of the plugin system. I'm using dlls to encapsulate various clients that the executable can load and run. Say I have a dll that creates messageboxes. I send it the paramaters and it returns a client that does all of the things that a message box can do. Is it better to load the dll again for each client such that each client exists in the memory space of its respective dll, or is it better to load the dll once, and use the same one to keep creating clients? The problem emerges during client deletion. I get the feeling that it isn't proper for the dll to new something and the executable to delete it. Thats just not good programming. So, How can I get the dll to delete the right instance of the client that I wish to delete? Is it ok for the executable to just send over a pointer to the dll and have the dll delete it? Or must the dll keep a record of each client it creates so that when the executable requests a deletion, the dll has to matchup the clients and delete its own copy? How do YOU implement your plugin system? Do you reference count? how do you cleanup?

Share this post


Link to post
Share on other sites
Quote:
Original post by aaron_ds
I'm in the process of rewriting bits and peices of my project code in an attempt to streamline the classes and managers and whatnot.
I'm thinking about reworking some of the plugin system. I'm using dlls to encapsulate various clients that the executable can load and run.
Say I have a dll that creates messageboxes. I send it the paramaters and it returns a client that does all of the things that a message box can do.
Is it better to load the dll again for each client such that each client exists in the memory space of its respective dll, or is it better to load the dll once, and use the same one to keep creating clients?
The problem emerges during client deletion. I get the feeling that it isn't proper for the dll to new something and the executable to delete it. Thats just not good programming.


The problem is that new and delete are implemented differently on different compilers. So if the DLL gets built with say, Visual C++, and the program gets built with GNU's C Compiler, it will crash horribly. The alternative is to create a wrapper function such as "FreeWhatever" that just deletes the thing in question itself, and put that function in the DLL.

Quote:
So, How can I get the dll to delete the right instance of the client that I wish to delete?
Is it ok for the executable to just send over a pointer to the dll and have the dll delete it?


Im having a hard time following exactly what your architecture is... could you try to explain it better? maybe an outline?

Quote:
Or must the dll keep a record of each client it creates so that when the executable requests a deletion, the dll has to matchup the clients and delete its own copy?
How do YOU implement your plugin system? Do you reference count? how do you cleanup?


The plugin systems I'm aquainted with implement each plugin as a DLL, which is then loaded by the main program, implemented through an interface which could potentially be put into a DLL and reused for multiple projects. There wouldn't be much need to reference count unless plugins can depend on plugins.

Share this post


Link to post
Share on other sites
new/delete: Ah yes. Thats an excelent reason why I need to be newing and deleting inside the dll.


The design is setup like so (bottom-up)


CLIENT* CreateInstance(int argc, char * argv[])
DLL |
---------------------------------------------------|-----------
EXECUTABLE |
application manager -----------------plugin manager
responsible for messagequeuing, responsible for loading and unloading dlls
initializing and closing clients passes created clients to appman
|
various kernel tasks
Video, Input, Sound, Client Manager <--Derivatives of baseclassTASK
|__|__|__| TASK contains static* to the application manager
|
kernel
|
application entry point

I'd like to increment a count for each dll everytime a client is newed, and decrement it everytime a client is deleted. when the count reaches 0 I'd like to free the dll.

Share this post


Link to post
Share on other sites
Quote:

Original Quote by MaulingMonkey
The problem is that new and delete are implemented differently on different compilers. So if the DLL gets built with say, Visual C++, and the program gets built with GNU's C Compiler, it will crash horribly.


the compilers implementation of new/delete are the last thing
to worry about in this situation.

foremost, and most importantly is that the Application and the
DLL have completly seperate Heaps, if you allocation on the
App heap, then Dealloc on the DLL heap or vice versa, you will
be completly trashing _somethings_ heapspace (maybe not even
your programs :p)

Cheers,
Danu

Share this post


Link to post
Share on other sites
A minimal COM based approach comes in handy.
You could use GUIDs to to identify your plugin's as well as unique type identifiers. This way you could also use severl plugins for the same type of operation.

Provide a common interface for all plugins (that would also handle life-time control - I'd also go for a Load()/Free() approach, letting the DLL handle new/delete internally) and provide a specialised interface for each plugin type.

IMHO it's also important to have a version information for your plugins.
Some kind of COM-lite would be a good start, I think.

Be careful with clean-up, however. Do not use the DLL's entry function to query unloading. I have had some really bad experience with that, as Windows seems to load/unload DLLs at application runtime quite randomly sometimes (any hints on that?).

It should be not much pain to implement a plugin-holder that keeps track of clients and cleans up DLLs automatically at shutdown. This is especially useful since some plugins might require a special destruction order (e.g. release client plugins that use gfx server facilities and resources before releasing the server itself).

Hope this makes sense,
Pat

Share this post


Link to post
Share on other sites

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this