tags:

views:

84

answers:

3

Hello, I have an application which Should be converted to a library. I' ve only copied the project dpr and changed the source file:

library aLibrary;
  uses
  FastMM4, 
  Forms,
  SysUtils,
  Windows,
  Mainfrm in 'Mainfrm.pas' {Mainform};
{$R *.res}
Procedure DllMain(Reason: Integer);
Begin
  If Reason = DLL_PROCESS_ATTACH Then
    Begin
      Application.Initialize;
      Application.CreateForm(TMainForm, MainForm);
      ExitCode := 0;
    End;
  If Reason = DLL_PROCESS_DETACH Then
    Begin
      Application.Terminate;
      MainForm.Free;
    End;
End;
Begin
  DllProc := @DllMain;
  DllProc(DLL_PROCESS_ATTACH);
End.

As you can see, I've simply removed the usually auto-generated code lines related to the app initialization and put them in the DllMain procedure, changed the 'program' keyword to 'library'. The dll loads well, the embedded program runs well too, but I can't manage to free it (FreeLibrary) in the host process. The dll freezes, whatever the DLL_PROCESS_DETACH code is (even when nothing is put for this case).

What would be the proper way to free all the application stuffs ?

+8  A: 

You're doing way too much in your DLL procedure.

While loading a DLL, the OS acquires the loader lock. That prevents any other libraries from being loaded at the same time. If you call a function that's in a library that hasn't already been loaded, then that will trigger an attempt to load that library. Since you're still inside the loader lock, that other library blocks while attempting to acquire the lock itself, and you get deadlock. Similar rules apply for unloading.

You should do the absolute minimum in the DllMain function to get your library loaded or unloaded. Leave everything else to a separate function that the DLL host can call after loading is complete or just before unloading begins. In your case, the minimum is probably nothing at all.

Rob Kennedy
Well done.Raymond Chen wrote about it and more here http://blogs.msdn.com/b/oldnewthing/archive/2004/01/28/63880.aspx
ChristianWimmer
A: 

Ok, I've well understood your summary about dll locks and I've put the embedded application constructor/destructor in some standard exported dll routines, so there 's no more DllMain. However calling freelibrary still reveals a deadlock. My dll is instantiated by another dll, which is itself instantiated in an executable. Maybe the lock is introduced at a lower level in the processes hierarchy.

bzx
Welcome to Stack Overflow. Please remember to log in using the same account you used to ask the question. When you do that, you're allowed to post *comments* attached to the answers. That way, others involved with the question will be notified that something happened. What you've posted here isn't really an *answer* to the question you posted, so it is out of place. This should have been posted as a comment. You can use the "contact us" link at the bottom of the page to contact an admin who can help merge your two accounts into one.
Rob Kennedy
A: 

How to debug application's hang.

Alexander