Solved

CallbackonCollectedDelegate was detected

Posted on 2011-02-17
2
503 Views
Last Modified: 2012-06-22
CallbackonCollectedDelegate was detected
A callback was made on a garbage collected delegate of type 'Sidebar!Sidebar.Acsr+DdeDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

I have created this private variable
private static DdeDelegate _callback;

Then later in the code I do this
                    _callback = new DdeDelegate(this.DdeDelegateMethod);                  
                    DdeInitialize(ref _lInstID, _callback, APPCMD_CLIENTONLY | MF_SENDMSGS | MF_POSTMSGS, 0);

I'm running out of ideas....any suggestions?

public class Acsr
    {
        #region Private Members

        private string _phoneNumber;
        private string _accountNumber;
        private string _errors;
        private static int _lInstID;
        private static int _hService;
        private static int _hTopic;
        private static int _hDDEConv;
        private static int _return;
        private static DdeDelegate _callback;

        #endregion

        #region Public Properties

        /// <summary>
        /// The customer's phone number
        /// </summary>
        public string PhoneNumber
        {
            get { return _phoneNumber; }
            set { _phoneNumber = value; }
        }

        /// <summary>
        /// The customer's account number
        /// </summary>
        public string AccountNumber
        {
            get { return _accountNumber; }
            set { _accountNumber = value; }
        }

        /// <summary>
        /// Any generated errors while trying pop the customer information into Acsr
        /// </summary>
        public string Errors
        {
            get { return _errors; }
        }

        #endregion

        #region Win32 API Implementation

        delegate IntPtr DdeDelegate(uint uType, uint uFmt, IntPtr hconv, IntPtr hsz1, IntPtr hsz2, IntPtr hdata, UIntPtr dwData1, UIntPtr dwData2);

        private IntPtr DdeDelegateMethod(uint uType, uint uFmt, IntPtr hconv, IntPtr hsz1, IntPtr hsz2, IntPtr hdata, UIntPtr dwData1, UIntPtr dwData2)
        {
            return IntPtr.Zero;
        }

        [DllImport("user32.dll")]
        static extern int DdeInitialize(ref int pidInst, DdeDelegate pfnCallback, int afCmd, int ulRes);

        [DllImport("user32.dll")]
        static extern int DdeCreateStringHandle(int idInst, string psz, int iCodePage);

        [DllImport("user32.dll")]
        static extern bool DdeFreeStringHandle(int idInst, int hsz);

        [DllImport("user32.dll")]
        static extern int DdeConnect(int idInst, int hszService, int hszTopic, int pCC);

        [DllImport("user32.dll")]
        static extern int DdeClientTransaction(string pData, int cbData, int hConv, int hszItem, int wFmt, int wType, int dwTimeout, ref int pdwResult);

        [DllImport("user32.dll")]
        static extern bool DdeFreeDataHandle(int hData);

        [DllImport("user32.dll")]
        static extern bool DdeDisconnect(int hConv);
        #endregion

        #region Constants
        private const int CP_WINANSI = 1004;
        private const int XCLASS_FLAGS = 0x4000;
        private const int XTYP_EXECUTE = 0x50 | XCLASS_FLAGS;
        private const int APPCMD_CLIENTONLY = 0x00000010;
        private const int MF_SENDMSGS = 0x2000000;
        private const int MF_POSTMSGS = 0x4000000;
        private const int TIME_OUT = 30000;
        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of the Acsr class
        /// </summary>
        public Acsr() { }

        /// <summary>
        /// Creates a new instance of the Acsr class with the customer's phone number and account number
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <param name="accountNumber"></param>
        public Acsr(string phoneNumber, string accountNumber)
        {
            _phoneNumber = phoneNumber;
            _accountNumber = accountNumber;
        }
        #endregion

        #region Public Methods

        /// <summary>
        /// Populates the Acsr billing screen with the customer's info
        /// </summary>
        public void PopCustomerInfo()
        {
            try
            {
                if (_phoneNumber == null)
                    _phoneNumber = string.Empty;

                if (_accountNumber == null)
                    _accountNumber = string.Empty;

                bool hasAccountNum = false;

                if (_phoneNumber.Length != 10)
                {
                    if (_accountNumber.Length == 0)
                    {
                        _errors = "No phone number or account number";
                        return;
                    }
                    else if (_accountNumber.Length > 0)
                    {
                        hasAccountNum = true;
                    }
                }
                else if (_accountNumber.Length == 16)
                {
                    hasAccountNum = true;
                }

                if (_phoneNumber.Length != 10 && hasAccountNum == false)
                {
                    _errors = "No phone number or account number";
                    return;
                }

                if (_lInstID == 0)
                {
                    _callback = new DdeDelegate(this.DdeDelegateMethod);                   
                    DdeInitialize(ref _lInstID, _callback, APPCMD_CLIENTONLY | MF_SENDMSGS | MF_POSTMSGS, 0);

                    string service = "ACSR";
                    string topic = "System";

                    CreateStringHandles(ref service, ref topic);

                    if (_hDDEConv == 0)
                    {
                        _hDDEConv = CreateConnection();
                    }

                    //int ret;
                    string value;

                    if (_hDDEConv != 0)
                    {
                        if (hasAccountNum == false)
                        {
                            value = "[WinOpen(CustomerInfo,Phone," + _phoneNumber + ")]" + null;
                        }
                        else
                        {
                            value = "[WinOpen(CustomerInfo,Subscriber," + _accountNumber + ")]" + null;
                        }

                        int pdwResult = 0;
                        _return = DdeClientTransaction(value, value.Length + 1, _hDDEConv, 0, 0, XTYP_EXECUTE, TIME_OUT, ref pdwResult);

                        DdeFreeDataHandle(_return);
                        ReleaseStringHandles();

                        if (_hDDEConv != 0)
                        {
                            ReleaseConnection();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _errors = ex.Message;
            }

        }

        #endregion

        #region Private Methods

        private void CreateStringHandles(ref string service, ref string topic)
        {
            _hService = DdeCreateStringHandle(_lInstID, service, CP_WINANSI);
            _hTopic = DdeCreateStringHandle(_lInstID, topic, CP_WINANSI);
        }

        private void ReleaseStringHandles()
        {
            if (_hService != 0)
            {
                DdeFreeStringHandle(_lInstID, _hService);
            }

            if (_hTopic != 0)
            {
                DdeFreeStringHandle(_lInstID, _hTopic);
            }
        }

        private static int CreateConnection()
        {
            return DdeConnect(_lInstID, _hService, _hTopic, 0);
        }

        private void ReleaseConnection()
        {
            if (_hDDEConv != 0)
            {
                DdeDisconnect(_hDDEConv);
                _hDDEConv = 0;
                _lInstID = 0;
            }
        }

        #endregion
    }

Open in new window

0
Comment
Question by:JRockFL
2 Comments
 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 500 total points
ID: 34922369
0
 
LVL 8

Author Comment

by:JRockFL
ID: 34922463
no it does not sorry. i need more help with the sample code i have provided.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now