none
c++ com interop use c# dll memory leak RRS feed

  • Allgemeine Diskussion

  • Hello,

    I have a Problem with my c++ program that uses a c# dll as com- object. It seems to have a Memory leak. The used Memory increases slowly. Can anybody help me? How can I solve the Problem?

    I have attached a code snipped of the c# code and of the c++ dll that uses the c# dll.

    Thanks,

    Ralf

    c++ code:


    #import "C:\temp\test_con.tlb" named_guids

    bool TestCommunicator::Connect ()
    {

        long count = 0;
        unsigned char* data = NULL;

        HRESULT hr;
        hr = CoInitialize(NULL);

        p.CreateInstance(__uuidof(TestCon_Con::Test_Con));


        if (p->Connect())
        {
            while ( m_stop == false )
            {
                p->ReadPDOData3 (&count, &data);   // <-- when this function is called the Memory is slowly increasing
             }

            p->DisConnect();
            p.Release();
        }

        CoUninitialize();
    }


    c# code:

    using Syst
    em;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;


    namespace TestCon_Con
    {
        [Guid("C751C166-2094-4F1F-A47D-9556991F460B")]
        public interface TestInterface
        {
            [DispId(1)]
            bool Connect();

            [DispId(2)]
            bool DisConnect();

            [DispId(3)]
            byte[] GetBusData();

            [DispId(4)]
            void ReadPDOData3([In, Out] ref int nNumElements,
                              [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] ref byte [] bArray );

        }


        [Guid("47361D08-F31D-416A-B599-97F473ACC276"),
        InterfaceType(ComInterfaceType.InterfaceIsIDispatc  h)]
        public interface Test_Events
        {
        }

        [Guid("7A0E9679-FD21-486D-BAE7-9387F0C2B9F1"),
        ClassInterface(ClassInterfaceType.None),
        ComSourceInterfaces(typeof(Test_Events))]
        public class Test_Con : TestInterface
        {


            public bool Connect()
            {
                return true;
            }


            public bool DisConnect()
            {
                return true;
            }


            public byte[] GetBusData()
            {
                return null;
            }


            public void ReadPDOData3(ref int count, ref byte[] data)
            {
                data = new byte [15];
                for (int i = 0; i < 15; i++)
                {
                data[i] = i;
                }
                count = data.Length;
            }
        }
    }

    Dienstag, 1. Juli 2014 06:03

Alle Antworten