passing std::ofstream by reference to DLL 
Author Message
 passing std::ofstream by reference to DLL

I am unable to pass an std::ofstream reference to a dll -see example code
below (program crashes at runtime with an unhandled exception).  This
appears to be an instance of STL template class members which are not
exported.  I have looked at KB Q172396 and KB 168958, but neither seem to
provide a solution to this problem, which must be encountered frequently
(e.g. when overloading operator>> in a dll).  Does anyone have some advice?
Thanks in advance.
--
Richard Greenblatt
Source Signal Imaging, Inc.
2323 Broadway, Suite 102, San Diego CA 92102 USA
(voice) +1-619-234-9935 (fax) +1-619-234-9934

(ftp) ftp.cts.com/users/king/s/source[/incoming]
----------------------------------------
// example code
// first a 'local' class that works
#include <fstream>
class CStdtest1 {
public:
 void Write(std::ofstream &);

Quote:
};

void
CStdtest1::Write(std::ofstream &fout)
{
 fout << 0 << "\n";

Quote:
}

// this is the main program which call the 2 class procedures
int main(int argc, char* argv[])
{
 CStdtest MyTestDLL;
 CStdtest1 MyTestLocal;
 std::ofstream fout("c:\\temp\\stdtest.1");
 if (!fout){
  std::cout << "File open error.";
 } else {
  MyTestLocal.Write(fout); // this works
  MyTestDLL.Write(fout);  // this fails
  fout.close();
 }
 return 0;

Quote:
}

// here is the dll procedure
// This class is exported from the stdtest.dll
// STDTEST_API is defined as __declspec(dllexport)
// or__declspec(dllimport) as appropriate

#include <fstream>

class STDTEST_API CStdtest {
public:
 void Write(std::ofstream &);

Quote:
};

void
CStdtest::Write(std::ofstream &fout)
{
 fout << 0 << "\n";
Quote:
}



Fri, 24 May 2002 03:00:00 GMT  
 passing std::ofstream by reference to DLL
Make sure that you are linking against the multi-threaded version of
the C-runtime dll -- the single threaded version can result in more
than one copy of the C runtime dll, which is usually bad.

To link against the multi-threaded C runtime dll, use the /MD switch
(or /MDd switch for debugging) in the linker options, in both the dll
and exe project.

With the single threaded c-runtime dll (the default), the dll and the
exe can end up with two copies of the dll's global variables, including
the default locale variables (used by strings and iostreams), and wierd
stuff can happen when something is allocated in an exe and freed in a
dll, or vice versa.  Passing a reference to an stl container to a dll
and adding to it can cause this problem.  The memory allocated in the
dll is actually allocated on a different heap, and the pointer is
invalid when passed to the exe for deletion.

So, in a nutshell, the behavior of the single threaded C runtime
library is really stupid, and you should instead link against the
multi-threaded version (even if you don't use multi-threading).

--
Steve Molitor

"Emacs is the Computer"

Quote:

> I am unable to pass an std::ofstream reference to a dll -see example code
> below (program crashes at runtime with an unhandled exception).  This
> appears to be an instance of STL template class members which are not
> exported.  I have looked at KB Q172396 and KB 168958, but neither seem to
> provide a solution to this problem, which must be encountered frequently
> (e.g. when overloading operator>> in a dll).  Does anyone have some advice?
> Thanks in advance.
> --
> Richard Greenblatt
> Source Signal Imaging, Inc.
> 2323 Broadway, Suite 102, San Diego CA 92102 USA
> (voice) +1-619-234-9935 (fax) +1-619-234-9934

> (ftp) ftp.cts.com/users/king/s/source[/incoming]
> ----------------------------------------
> // example code
> // first a 'local' class that works
> #include <fstream>
> class CStdtest1 {
> public:
>  void Write(std::ofstream &);
> };

> void
> CStdtest1::Write(std::ofstream &fout)
> {
>  fout << 0 << "\n";
> }

> // this is the main program which call the 2 class procedures
> int main(int argc, char* argv[])
> {
>  CStdtest MyTestDLL;
>  CStdtest1 MyTestLocal;
>  std::ofstream fout("c:\\temp\\stdtest.1");
>  if (!fout){
>   std::cout << "File open error.";
>  } else {
>   MyTestLocal.Write(fout); // this works
>   MyTestDLL.Write(fout);  // this fails
>   fout.close();
>  }
>  return 0;
> }

> // here is the dll procedure
> // This class is exported from the stdtest.dll
> // STDTEST_API is defined as __declspec(dllexport)
> // or__declspec(dllimport) as appropriate

> #include <fstream>

> class STDTEST_API CStdtest {
> public:
>  void Write(std::ofstream &);
> };

> void
> CStdtest::Write(std::ofstream &fout)
> {
>  fout << 0 << "\n";
> }



Sat, 25 May 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. passing std::vector to a DLL by reference

2. Passing std::set and std::map objects between 2 dlls

3. ifstream/ofstream passed to a DLL

4. std::vector passed to DLL class never arrives???

5. HELP: Passing std::list to a DLL

6. Std::set and passing between DLLs

7. passing std::map based objects from an app to a dll

8. Converting a FILE* to std::ofstream ???

9. ATL component crashed in std::ofstream

10. Open a file with std::ofstream without truncate it

11. Control sharing for files opened in a std::ofstream object

12. Extraneous characters written to std::ofstream

 

 
Powered by phpBB® Forum Software