?
Solved

CallbackonCollectedDelegate was detected

Posted on 2011-02-17
2
Medium Priority
?
514 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

741 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