Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

DLL loading issues

This topic is 3618 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

im havin some issues with my dll loading... i have an interface class called "GraphicsDevice" which i then implement in a dll and use a factory method to get an instance from the dll... to load the dll i create a seperate class "GraphicsDeviceLibrary" which loads the dll (LoadLIbrary/FreeLibrary) and stores the HMODULE/HINSTANCE (whatever u wanna call it)... however this doesnt seem to work well... first of all i have two different objects... which i dont like.. secondly there seem to be some memory leak issues with this... thirdly i always have to make sure to free the GraphicsDevice instance before the GraphicsDevice Library instance... what is the proper way to do this? EDIT:
#include "Library.hpp"

#include "GraphicsDevice.hpp"

#include <iostream>
#include <windows.h>

#include <boost/shared_ptr.hpp>

namespace drone
{
	namespace graphics
	{

		struct Library::Implementation
		{
			typedef graphics::IGraphicsDevice* (*CreateGraphicsDevice)(application::win32::WindowPtr);
		public:

			Implementation() : hInstGraphicsLibrary(NULL)
			{
			}

			~Implementation()
			{
				if (hInstGraphicsLibrary)
					FreeLibrary(hInstGraphicsLibrary);
			}

			graphics::IGraphicsDevicePtr LoadGraphicsDevice(const std::wstring& name, application::win32::WindowPtr window)
			{
				graphics::IGraphicsDevice* pGraphicsDevice;

				hInstGraphicsLibrary = LoadLibrary(name.c_str());
				if (hInstGraphicsLibrary)
				{
					CreateGraphicsDevice CreateGraphicsDeviceFunc = (CreateGraphicsDevice)GetProcAddress(hInstGraphicsLibrary, "CreateGraphicsDevice");

					if (CreateGraphicsDeviceFunc)
						pGraphicsDevice = CreateGraphicsDeviceFunc(window);	
					else
						throw std::exception("Failed to get function address.");
				}
				else
				{
					throw std::exception("Failed to load library");
				}

				return graphics::IGraphicsDevicePtr(pGraphicsDevice);
			}

		private:

			HMODULE hInstGraphicsLibrary;
		};

		Library::Library() : pImpl_(new Implementation)
		{
			std::wcout << ">> Created GraphicsDeviceLoader. (Drone)\n";
		}


		graphics::IGraphicsDevicePtr Library::LoadGraphicsDevice(const std::wstring& name, application::win32::WindowPtr window)
		{
			return pImpl_->LoadGraphicsDevice(name, window);
		}

	}
}

Share this post


Link to post
Share on other sites
Advertisement
All I can suggest is not returning a smart pointer to the DLL class directly, but using a proxy class instead. When that proxy class gets destroyed, you know the last smart pointer reference has gone, and you can call FreeLibrary().

Alternatively, I don't know if you can do anything with Boost to automagically call FreeLibrary when the object pointed to is about to be deleted.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!