Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2004

Tip: Looking for answers? Try searching our database.

Unmanaged C++ DLL with STL causes crash

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kurt Smith - 10 Jun 2004 22:41 GMT
We are in the process of creating a managed C++ library to wrap some
existing unmanaged code.  The unmanaged code makes use of the STL.  We
are seeing NULL pointer exceptions whenever we construct our unmanaged
object.  If we remove the dependency on STL in the unmanaged code the
crash goes away.  Also, we can always instantiate the unmanged object
the first time but it fails on subsequent attempts.  The managed code
is pretty simple.  has anyone else seen this behavior?

// SimpleManaged.h

#pragma once

#include "./import/simpleunmanaged.h"
#pragma comment(lib, "./import/simpleunmanaged.lib")

public __gc class CSimpleManaged
{
private:
    CSimpleUnmanaged __nogc* m_pSimpleUnmanaged;

public:
    CSimpleManaged(void)
        : m_pSimpleUnmanaged(new CSimpleUnmanaged())
    {
    }

    ~CSimpleManaged(void)
    {
        //System::Console::WriteLine(S"~CSimpleManaged");
        if(NULL != m_pSimpleUnmanaged)
        {
            delete m_pSimpleUnmanaged;
            m_pSimpleUnmanaged = NULL;
        }
    }

    int Sum(int p_iData)
    {
        int rc = m_pSimpleUnmanaged->Sum(p_iData);
        return(rc);
    }

    System::String* Concat(System::String* p_strData)
    {
        System::IntPtr ipStrData =
System::Runtime::InteropServices::Marshal::StringToHGlobalUni(p_strData);
        wchar_t* pszData = static_cast<wchar_t*>(ipStrData.ToPointer());

        wchar_t* pszResult = m_pSimpleUnmanaged->Concat(pszData);
        System::String* strResult = new System::String(pszResult);

        System::Runtime::InteropServices::Marshal::FreeHGlobal(ipStrData);

        return(strResult);
    }
};
Nadav - 12 Jun 2004 23:48 GMT
Well, mixed mode DLLs are linked without an entry point so _DLLmainCRTStartup is not being called and CRT is not being initialized so static variables are not set, it is possible that the STL classes used by you use some static variables, if those variables are not initialized you would probably get errors during runtime, a detailed explanation of how to resolve this problem could be fond at the following URL: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/vcco
nconvertingmanagedextensionsforcprojectsfrompureintermediatelanguagetomixedmode.
asp


Signature

Nadav
  Sofin l.t.d.

> We are in the process of creating a managed C++ library to wrap some
> existing unmanaged code.  The unmanaged code makes use of the STL.  We
[quoted text clipped - 52 lines]
>     }
> };

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.