Solved

CallbackonCollectedDelegate was detected

Posted on 2011-02-17
2
504 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fomat Json objects 6 28
Need help on C# Linq query on object 6 53
Word Directory is not in the drop down list 4 26
C#  Radio button search for Date not DateTime 4 35
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

864 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

22 Experts available now in Live!

Get 1:1 Help Now