CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2
  • Faltering Windows support
  • Internet Explorer 8 Click Clever Click Safe
  • Release Candidate 2 for ASP.NET MVC 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C-Sharp Programming Post questions, answers, and comments about C#.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old August 3rd, 2009, 10:32 PM
    dieucay555 dieucay555 is offline
    Junior Member
     
    Join Date: May 2007
    Posts: 4
    dieucay555 is an unknown quantity at this point (<10)
    Delegate Proplem

    I trying use 1 DLL write by C++ in project C# but i hava a proplem!
    In C++, when i create 1 event, using callback.For example : create event MouseMove : create 1 procedure is void CALLBACK MouseMoveProc(), and to handle events that it uses CadOnEventMouseMove(MouseMoveProc);in the form CadOnEventMouseMove void CadOnEventMouseMove (
    F_MOUSEMOVE pFunc / / pointer to event procedure
    );

    To subscribe to the events which, in C # you do the following:
    1 declare delegate to take the place of cursor to the function:
    public delegate void MouseDelegate(int hDwg, int Button, int Key, int Xwin, int Ywin, double Xdwg, double Ydwg, double Zdwg);

    And then, create event :
    CadOnEventMouseMove(new VeCAD.MouseDelegate(MouseMoveProc));

    When compiled no error but a few seconds after the application is crash, and errors arising :
    "A callback was made on a garbage collected delegate of type 'VeCad_CS1!VeCAD.MouseDelegate::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."

    Please help me!
    Reply With Quote
      #2    
    Old August 4th, 2009, 12:23 AM
    vcdebugger vcdebugger is offline
    Member
     
    Join Date: May 2009
    Posts: 288
    vcdebugger will become famous soon enough (50+)
    Re: Delegate Proplem

    Sorry I could not clearly understand your english.
    You mean to say you have written the handler in a c++ dll ? and trigerring the event happens in the C# application?
    Reply With Quote
      #3    
    Old August 4th, 2009, 11:46 AM
    monalin monalin is offline
    Member
     
    Join Date: Jul 2006
    Posts: 297
    monalin has a spectacular aura about (125+) monalin has a spectacular aura about (125+)
    Re: Delegate Proplem

    Never seen this error before or tried to do what you're doing so i may not be that much help. You might try the following code though. Its possible that the GC doesn't like you instanciating a new object in the parameter of an unmanaged function. You might need to bring the

    Code:
    new VeCAD.MouseDelegate(MouseMoveProc)
    into a different scope so the GarbageCollector doesn't flip out.

    Heres the new code you can try

    Code:
        public delegate void MouseDelegate(int hDwg, int Button, int Key, int Xwin, int Ywin, double Xdwg, double Ydwg, double Zdwg);
        public event MouseDelegate MouseMove;
    
        MouseMove += new MouseDelegate(MouseMoveProc);
        CadOnEventMouseMove(MouseMove);
    Reply With Quote
      #4    
    Old August 4th, 2009, 04:53 PM
    JonnyPoet's Avatar
    JonnyPoet JonnyPoet is offline
    Elite Member
    Power Poster
     
    Join Date: Mar 2005
    Location: Vienna, Austria
    Posts: 4,371
    JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+) JonnyPoet has a brilliant future (2000+)
    Re: Delegate Proplem

    Quote:
    Originally Posted by dieucay555 View Post
    ..."A callback was made on a garbage collected delegate of type 'VeCad_CS1!VeCAD.MouseDelegate::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."
    This states that you have instantiated a delegate without having it alive after you created it. Because you created itin the method parameter you do not point to this delegate anywhere in your program. Understand: Your delegate points to a method But the delegate itself is not stored in any field of that class so it is created and when you are leaving the method where you created it, this delegate goes to garbage colection, because nothing points to it !!
    Code:
    private void AnyMethodSettingTheDelegate(){
       // because you create it directly as the callback method parameter
      // you have no chance to have the pointer to this delegate
      // method stored anywhere in your class. So this runs out of 
      // scope instantly when the method is finished
       CadOnEventMouseMove(new VeCAD.MouseDelegate(MouseMoveProc));
    ...
    } // just when the method is finished the delegate is out of scope !!!
    In the code shown by monalin
    Code:
    namespace VeCad{
        // define the delegate
        public delegate void MouseDelegate(int hDwg, int Button, int Key, int Xwin, int Ywin, double Xdwg, double Ydwg, double Zdwg);
     
       public class VeCad{
         // create a field in your class so you have the delegate living as long as this class lives   
          public event MouseDelegate MouseMove;
         // in the code wherever it is needed maybe in the classes CTor or 
        // where you want to set the callback you initialize the delegate to this adress
         MouseMove += new MouseDelegate(MouseMoveProc);
         // and now you set it to your method that uses the callback
        CadOnEventMouseMove(MouseMove);
    This way your delegate lives as long as this class lives. Hope you get the difference why this is needed
    __________________
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 12:38 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.