Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CallbackonCollectedDelegate was detected

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 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