Wrapping Unmanaged Class with std::fstream object fails 
Author Message
 Wrapping Unmanaged Class with std::fstream object fails

Wrapping an unmanaged class that has an fstream property
fails.  Other tested std objects work fine e.g. vector.

I have a real example where the problem occurs during
object construction.  The included example fails during
destruction of the unmanaged object inside the destructor
of the fstream object.  This is the error output:

First-chance exception at 0x77f69ecd in fstream.exe:
0xC0000005: Access violation writing location 0x00000010.
An unhandled exception of
type 'System.NullReferenceException' occurred in
fstream.exe

The failing example follows:

#using <mscorlib.dll>
using namespace System;

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

class UnmanagedFstream
{
public:
        UnmanagedFstream(void)
        {
        }

        ~UnmanagedFstream(void)
        {
        }

private:
#if 1 // works if 0
        fstream f;
#else
        vector<float> f;
#endif

Quote:
};

public __gc class ManagedFstream
{
private:
        UnmanagedFstream __nogc * m_pFstream;

public:

        ManagedFstream(void)
        {
                m_pFstream = new UnmanagedFstream();
        }

        ~ManagedFstream()
        {
                delete m_pFstream;
        }

Quote:
};

int _tmain()
{
    ManagedFstream *mf = new ManagedFstream();
        return 0;
Quote:
}



Sat, 26 Nov 2005 03:14:49 GMT  
 Wrapping Unmanaged Class with std::fstream object fails
Wrapping an unmanaged class that has an fstream property
fails.  Other tested std objects work fine e.g. vector.

I have a real example where the problem occurs during
object construction.  The included example fails during
destruction of the unmanaged object inside the destructor
of the fstream object.  This is the error output:

First-chance exception at 0x77f69ecd in fstream.exe:
0xC0000005: Access violation writing location 0x00000010.
An unhandled exception of
type 'System.NullReferenceException' occurred in
fstream.exe

The failing example follows:

#using <mscorlib.dll>
using namespace System;

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

class UnmanagedFstream
{
public:
        UnmanagedFstream(void)
        {
        }

        ~UnmanagedFstream(void)
        {
        }

private:
#if 1 // works if 0
        fstream f;
#else
        vector<float> f;
#endif

Quote:
};

public __gc class ManagedFstream
{
private:
        UnmanagedFstream __nogc * m_pFstream;

public:

        ManagedFstream(void)
        {
                m_pFstream = new UnmanagedFstream();
        }

        ~ManagedFstream()
        {
                delete m_pFstream;
        }

Quote:
};

int _tmain()
{
    ManagedFstream *mf = new ManagedFstream();
        return 0;
Quote:
}



Fri, 02 Dec 2005 23:09:41 GMT  
 Wrapping Unmanaged Class with std::fstream object fails
Hi Peter,

Would you please tell me how to reproduc your problem on my side step by
step? I certainly appreciate your time.

Best regards,

Jacob Yang
This posting is provided "AS IS" with no warranties, and confers no rights.



Sat, 03 Dec 2005 16:47:33 GMT  
 Wrapping Unmanaged Class with std::fstream object fails

Quote:

> Subject: Wrapping Unmanaged Class with std::fstream object fails

> Wrapping an unmanaged class that has an fstream property
> fails.  Other tested std objects work fine e.g. vector.

> I have a real example where the problem occurs during
> object construction.  The included example fails during
> destruction of the unmanaged object inside the destructor
> of the fstream object.  This is the error output:

> First-chance exception at 0x77f69ecd in fstream.exe:
> 0xC0000005: Access violation writing location 0x00000010.
> An unhandled exception of
> type 'System.NullReferenceException' occurred in
> fstream.exe

> The failing example follows:

> #using <mscorlib.dll>
> using namespace System;

> #include <iostream>
> #include <fstream>
> #include <vector>

> using namespace std;

> class UnmanagedFstream
> {
> public:
>    UnmanagedFstream(void)
>    {
>    }

>    ~UnmanagedFstream(void)
>    {
>    }

> private:
> #if 1 // works if 0
>    fstream f;
> #else
>    vector<float> f;
> #endif
> };

> public __gc class ManagedFstream
> {
> private:
>    UnmanagedFstream __nogc * m_pFstream;

> public:

>    ManagedFstream(void)
>    {
>            m_pFstream = new UnmanagedFstream();
>    }

>    ~ManagedFstream()
>    {
>            delete m_pFstream;
>    }
> };

> int _tmain()
> {
>     ManagedFstream *mf = new ManagedFstream();
>    return 0;
> }

Peter,

This is what appears to be happening: finalization of the managed class
(which call ~ManagedFstream) is occurring after the C runtime has completed
its shutdown code. As a result, the internal structures needed to properly
delete the UnmanagedFstream have been released.

If it's possible to explicitly delete the unmanaged pointers held by the
managed class before exit, that might be the way to go. Alternately, you
could possibly watch for AppDomainUnload events to trigger that release.

For example, adding an explicit "delete mf;" to _tmain() will avoid the
fault.

We are aware of these problems in the coordination of managed and unmanaged
shutdown code and are working to address them in a future release.

--
Mark Levine, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm



Mon, 05 Dec 2005 02:05:27 GMT  
 Wrapping Unmanaged Class with std::fstream object fails

Quote:

> Subject: Wrapping Unmanaged Class with std::fstream object fails

> Wrapping an unmanaged class that has an fstream property
> fails.  Other tested std objects work fine e.g. vector.
> ...

Peter,

I posted a response today to your earlier post about this problem (under
the same subject line).

The problem in your case is that the C runtime's shutdown runs before the
Finalization code runs on the ManagedFstream object. As a result,
~ManagedFstream calls ~UnmanagedFstream which tries to access C runtime
resources that have already been released.

We are aware of coordination problems between the C runtime and the
Finalizer thread. We are working on addressing them in a future release.

In the meantime, you can avoid the problem by explicitly deleting managed
objects that hold pointers to unmanaged objects. For example, adding
"delete mf;" to _tmain() in your example.

--
Mark Levine, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm



Mon, 05 Dec 2005 02:38:43 GMT  
 Wrapping Unmanaged Class with std::fstream object fails
Thanks Mark,

delete mf does the trick.

Peter



Mon, 05 Dec 2005 22:09:40 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Creating verifiable dotnet assembly wrapping unmanaged C++ classes

2. Help me!! wrap unmanaged class with managed c++

3. Unmanaged C++ class created from CSharp or VB fail with no error

4. Managed Class Containing Unmanaged Objects Does Not Dispose

5. Why wrapping unmanaged C++ singletons can be dangerous

6. Wrapping unmanaged operator overloads?

7. How to read write file with std fstream

8. incompatibility between namespace std and fstream library

9. std::fstream & wchar_t

10. Using CArchive handle for std::fstream

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

12. Casting manged object to unmanaged object

 

 
Powered by phpBB® Forum Software