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++ / October 2007

Tip: Looking for answers? Try searching our database.

C# to CLI call fails somewhere...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joachim - 05 Oct 2007 09:20 GMT
I have the below C# definitions and function call to an unsafe C++/CLI
function in an external dll. When debugging the calls step by step everything
seems to work, but if I just run everything through without stopping I get
the following error:

"
Debug Assertion Failed!

Program: .....myprogram..
File: dbgheap.c
Line: 1279

Expression: _CrtIsValidHeapPointer(pUserData)

...
"

If I press ignore three times the whole application crashes. Here is the code:

       [DllImport(g_wrapper_dll_path)]
       unsafe static extern int GetEncoders(
           void* dvpsdk,
           ref int[] devices_ids);

       IntPtr m_DVPSDK_ptr;
               void* l_ptr = m_DVPSDK_ptr.ToPointer();
               int l_result = GetEncoders(
                   l_ptr,
                   ref m_devs_ids);

The C++/CLI functions:

    extern "C" int GetEncoders(
        System::IntPtr dvpsdk,
        array<int>^ devices_ids)
    {
        return AdvantechImpl::GetEncoders(
            (DVP1412DLL*) dvpsdk.ToPointer(),
            devices_ids);
    }

    const int GetEncoders(
        DVP1412DLL* dvpsdk,
        array<int>^ devices_ids)
    {
        int l_nr_of_devices =
            dvpsdk->DVP1412_GetNoOfDevices();

        if (l_nr_of_devices == 0)
        {
            return -3;
        }

        int* l_tmp = new int[devices_ids->Length];

        int l_res = dvpsdk->DVP1412_InitSDK(
            l_nr_of_devices,
            l_tmp);

        if (l_res == SUCCEEDED)
        {
            for (int l_i = 0; l_i < devices_ids->Length; ++l_i)
            {
                devices_ids[l_i] = l_tmp[l_i];
            }

            l_res = dvpsdk->DVP1412_CloseSDK();
            return l_nr_of_devices;
        }
        else
        {
            return l_res;
        }
    }
Joachim - 05 Oct 2007 10:21 GMT
I should add that m_devs_ids has the following definition:

       [MarshalAs(UnmanagedType.ByValArray, SizeConst = g_max_devices)]
       int[] m_devs_ids = new int[g_max_devices];

> I have the below C# definitions and function call to an unsafe C++/CLI
> function in an external dll. When debugging the calls step by step everything
[quoted text clipped - 70 lines]
>         }
>     }
Ben Voigt [C++ MVP] - 05 Oct 2007 18:48 GMT
>I have the below C# definitions and function call to an unsafe C++/CLI
> function in an external dll. When debugging the calls step by step
[quoted text clipped - 56 lines]
> l_nr_of_devices,
> l_tmp);

When l_nr_of_devices > devices_ids->Length, this function overflows the
buffer and corrupts the heap.

Also, where is l_tmp freed (you must call delete[])?

> if (l_res == SUCCEEDED)
> {
[quoted text clipped - 11 lines]
> }
> }

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.