?
Solved

CallbackonCollectedDelegate was detected

Posted on 2011-02-17
2
Medium Priority
?
518 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

839 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