Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.

Public Report
Report From: Delphi-BCB/RTL/C++    [ Add a report in this area ]  
Report #:  114068   Status: Open
Application may crashes when a DLL is loaded twice
Project:  C++Builder Build #:  17.0.4723.55752
Version:    17.1 Submitted By:   Paul F
Report Type:  Crash / Data loss / Total failure Date Reported:  3/22/2013 8:48:25 AM
Severity:    Infrequently encountered problem Last Updated: 3/24/2013 1:55:50 AM
Platform:    All versions Internal Tracking #:   37506
Resolution: None (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: 50
Description
Application may crashes when a DLL is loaded twice

Using some units on a static library will make the loading of a consumer DLL always crash, by the second load.

Using
  Link with Dynamic RTL = True
  Link with Runtime Packages = True
Steps to Reproduce:
1) Create a static library
File, New, Other, C++Builder Projects, Static Library, Ok
File, New, Unit - C++Builder
------
#include <vcl.h>
#pragma hdrstop
#include "uSomeStaticLib.h"
#include <Themes.hpp>
#include <DBTables.hpp> // required for crash...
#pragma package(smart_init)
void foo()
{ // any StyleServices() calls like...
  Themes::StyleServices()->ApplyThemeChange();
}
------
Shift+Ctrl+S
uSomeStaticLib.cpp
SomeStaticLib.cbproj

Project, Options, TLib, Link with Dynamic RTL = true, Ok
Shift+F9

2) Create a DLL that uses the above static library
File, New, Other, Dynamic-link Library, Ok [C++,VCL,VC++]
------
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#pragma comment(lib, "SomeStaticLib")
#pragma argsused

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
  extern void foo();
  if (!hinst) // dummy test, just to force linking
    foo(); // but no actual call required
  return 1;
}
------
Shift+Ctrl+S
uSomeDLL.cpp
SomeDLLPCH.h
SomeDLL.cbproj

Project, Options, Packages, Runtime Packages,
Expand "Runtime package import libraries", Inherited = false,
Set the above "Runtime package import libraries" as rtl;vcl;dbrtl;dsnap
Ok
Shift+F9

3) Create the application that calls the above DLL *twice*
File, New, VCL Forms Application - C++Builder
Drop a button on the main form
------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  if (HINSTANCE h = (HINSTANCE)SafeLoadLibrary("SomeDLL.DLL")) // Loads Ok
    FreeLibrary(h);
  if (HINSTANCE h = (HINSTANCE)SafeLoadLibrary("SomeDLL.DLL")) // Crash!
    FreeLibrary(h);
}
------
Shift+Ctrl+S
uSomeApp.cpp
SomeAppPCH.h
SomeApp.cbproj

Project, Options, Packages, Runtime Packages,
Expand "Runtime package import libraries", Inherited = false,
Set the above "Runtime package import libraries" as rtl;vcl;dbrtl;dsnap
Ok, F9

Project SomeApp.exe raised exception class $C0000005 with message 'access violation at 0x50061a36: write of address 0x504fdbe8'.
Workarounds
None
Attachment
QC_114068_.zip
Comments

Jean-Marie Babet at 3/25/2013 8:21:29 AM -
Hello,

I suspect, the problem here is that the static library does not *know* that it will end up using Packages. In the DBTable HPP file you'll see the following:

#ifdef USEPACKAGES
#pragma link "dbrtl.bpi"
#pragma link "vcldb.bpi"
#pragma link "vcl.bpi"
#else
#pragma link "dbrtl.lib"
#pragma link "vcldb.lib"
#pragma link "vcl.lib"
#endif

This code is sensitive to whether we will use runtime Packages or static linking. When the static library is built, USEACKAGES is not defined. So the abve causes the compiler to generate link records telling the linker to pull in static versions of dbrtl, vcldb and vcl. But then that code ends up in a module that should link to runtime packages. :(

I have not confirmed this but I suspect that we need to expose the runtime package settings even for a static library. We don't because Packages are thought of as a link-time setting, hence not relevant to static libraries. But for headers like DBRTL, that make link time decisions at compile time, that setting is important.

Cheers,

Bruneau

Paul F at 3/26/2013 5:09:12 AM -
Thank you. It helps.

Server Response from: ETNACODE01