]> Creatis software - gdcm.git/commitdiff
ENH: Adding an exe that seg fault on AMD64/gcc.
authormalaterre <malaterre>
Mon, 19 Sep 2005 18:54:10 +0000 (18:54 +0000)
committermalaterre <malaterre>
Mon, 19 Sep 2005 18:54:10 +0000 (18:54 +0000)
Testing/CMakeLists.txt
Testing/dynmodule.cxx [new file with mode: 0644]
Testing/loadmodule.cxx [new file with mode: 0644]

index 47f897bf19936d2f3c36765a530dc8a84f5eeec8..994e37bef00776e6ab8ba43c81b797afc6294562 100644 (file)
@@ -181,3 +181,17 @@ FILE(WRITE "${GDCM_BINARY_DIR}/gdcmDataImages.h"
 FILE(WRITE "${GDCM_BINARY_DIR}/gdcmDataSeqImages.h"
   "const char * const gdcmDataSeqImages[] = { ${GDCM_DATA_SEQ_IMAGES}\n0 };\n" 
 )
+
+#-----------------------------------------------------------------------------
+# Trying to solve a bizarre problem when loading shared lib if gdcm is static
+# Therefore I disable the test when building SHAREDLIBS
+IF(UNIX AND NOT GDCM_BUILD_SHARED_LIBS)
+  ADD_LIBRARY(dynmodule MODULE dynmodule.cxx)
+  TARGET_LINK_LIBRARIES(dynmodule gdcm)
+  ADD_EXECUTABLE(loadmodule loadmodule.cxx )
+  TARGET_LINK_LIBRARIES(loadmodule gdcm -ldl)
+  ADD_TEST(loadmodule ${CXX_TEST_PATH}/loadmodule)
+ENDIF(UNIX AND NOT GDCM_BUILD_SHARED_LIBS)
+
+
+
diff --git a/Testing/dynmodule.cxx b/Testing/dynmodule.cxx
new file mode 100644 (file)
index 0000000..2c9a664
--- /dev/null
@@ -0,0 +1,39 @@
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: dynmodule.cxx,v $
+  Language:  C++
+  Date:      $Date: 2005/09/19 18:54:10 $
+  Version:   $Revision: 1.1 $
+                                                                                
+  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+  l'Image). All rights reserved. See Doc/License.txt or
+  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
+                                                                                
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+                                                                                
+=========================================================================*/
+
+#include "gdcmFile.h"
+
+#ifdef _WIN32
+#  define __ELX_DLLEXPORT __declspec(dllexport)
+#else
+#  define __ELX_DLLEXPORT
+#endif
+
+extern "C" __ELX_DLLEXPORT void afunc(void)
+{
+  std::cerr << "Hello, we are in the dll-function afunc!" << std::endl;
+
+  // Create an instance of a gdcm class. This makes sure
+  // that the problematic gdcm-library is linked.
+  gdcm::File *file = new gdcm::File();
+  
+  std::cerr << "End of function afunc"  << std::endl; 
+  
+}
+
+
diff --git a/Testing/loadmodule.cxx b/Testing/loadmodule.cxx
new file mode 100644 (file)
index 0000000..2cf7de4
--- /dev/null
@@ -0,0 +1,93 @@
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: loadmodule.cxx,v $
+  Language:  C++
+  Date:      $Date: 2005/09/19 18:54:10 $
+  Version:   $Revision: 1.1 $
+                                                                                
+  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+  l'Image). All rights reserved. See Doc/License.txt or
+  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
+                                                                                
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+                                                                                
+=========================================================================*/
+/* For some bizarre reason global symbol, mainly std::string
+ * are creating seg fault when loading a shared lib, build on top
+ * of a static gdcm lib */
+
+#include "gdcmFile.h"
+#include <dlfcn.h>
+
+int main(int, char* [])
+{
+  //typedef itk::DynamicLoader dl;
+  typedef void* lh;
+  
+  // the type of function stored in the dynamic lib 
+  typedef void (*fnptr)(void);
+  
+  //This function gives a segmentation fault.
+  //std::cerr << "Last error before opening: " << dl::LastError() << std::endl;
+
+  //Fill in here  <path_to_lib>/libadll.so"
+  std::string libname = GDCM_LIBRARY_OUTPUT_PATH "/libdynmodule.so";
+
+  //open the dynamic lib
+  //lh handle =  dl::OpenLibrary( libname.c_str() );
+  lh handle =  dlopen(libname.c_str(), RTLD_LAZY);
+  std::cerr << "address of handle: " << handle << std::endl;
+  if (!handle)
+    {
+    std::cerr << "Ooops cannot open lib:" << libname << std::endl;
+    return 1;
+    }
+  //std::cerr << "Error after opening: " << dl::LastError() << std::endl;
+
+  
+  std::cerr << "Getting function address from lib" << std::endl;
+  void * aptr = dlsym(handle, "afunc");
+  
+  if (aptr == 0)
+  {
+    std::cerr << "error in finding function" << std::endl;
+    //const char *er = dlerror();
+    //std::cerr << "dl says: " << dl::LastError() << std::endl;
+
+    std::cerr << "Closing libs" << std::endl;
+    //dl::CloseLibrary(handle);
+    dlclose(handle);
+    //std::cerr << "dl says: " << dl::LastError() << std::endl;
+
+    std::cerr << "Exiting" << std::endl;
+    return 1;
+  }
+
+  
+  std::cerr << "Casting function address from lib" << std::endl;
+  fnptr afuncptr = (fnptr)(aptr);
+
+  std::cerr << "Calling function from lib" << std::endl;
+  afuncptr(); 
+
+  // Create an instance of the GDCMImageIO class. This makes sure
+  // that the problematic gdcm-library is linked.
+  //itk::GDCMImageIO::Pointer gdcmio = itk::GDCMImageIO::New();
+  gdcm::File *file = new gdcm::File();
+  
+  std::cerr << "Closing libs" << std::endl;
+  //dl::CloseLibrary(handle);
+  dlclose(handle);
+
+  //std::cerr << "dl says: " << dl::LastError() << std::endl;
+  
+  std::cerr << "Exiting" << std::endl;
+  return 0;
+  
+}
+
+