question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Application crash when using .ocx with events that have ref objects, works in Framework - System.Windows.Forms.AxHost

See original GitHub issue
  • .NET Core Version: 3.0 & 3.1

  • Have you experienced this same bug with .NET Framework?: No

Problem description:

Using ActiveX .ocx user control that has specifically not null, ref objects passed on events crashes entire application due to corrupt memory - Same code works in Framework

Expected behavior:

ref object is not corrupt on return to OCX after calling event inside core application

Minimal repro:

using System;
using System.Windows.Forms;

namespace ConsoleApp1
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var c = new WindowsFormsControlLibrary1.UserControl1();
            c.CreateControl();
            c.TestEventByRef += C_TestEventByRef;
            c.WithObjectByRefTestEvent();
            c.Dispose();

            var c2 = new AxAxolotl.AxTestControl();
            c2.CreateControl();
            c2.TestEventByRef += axTestControl1_TestEventByRef;
            c2.WithObjectByRefTestEvent();
            c2.Dispose();
        }

        private static void C_TestEventByRef(object sender, WindowsFormsControlLibrary1.TestEventByRefEventArgs e)
        {
            e.TestObject = new WindowsFormsControlLibrary1.TestObject { Text = "Framework Out" };
        }

        private static void axTestControl1_TestEventByRef(object sender, AxAxolotl.__TestControl_TestEventByRefEvent e)
        {
            MessageBox.Show(e.column.Text);

            e.column = new Axolotl.CTestObject { Text = "OCX Out" };
        }
    }
}

example winforms control that works in framework and core

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsControlLibrary1
{
    public class TestEventByRefEventArgs : EventArgs
    {
        public TestObject TestObject { get; set; }
    }

    public partial class UserControl1 : UserControl
    {
        public event EventHandler<TestEventByRefEventArgs> TestEventByRef;

        public UserControl1()
        {
            InitializeComponent();
        }

        public void WithObjectByRefTestEvent()
        {
            var testObject = new TestObject { Text = "In" };

            var testEventByRefEventArgs = new TestEventByRefEventArgs { TestObject = testObject };

            TestEventByRef?.Invoke(this, testEventByRefEventArgs);

            MessageBox.Show(testEventByRefEventArgs.TestObject.Text);
        }
    }
}

example OCX (in VB6) control that works in framework by crashes in core

Option Explicit

Event TestEventByRef(ByRef Column As CTestObject)

Public Sub WithObjectByRefTestEvent()
    Dim a As CTestObject
    Set a = New CTestObject
    a.Text = "OCX In"
    RaiseEvent TestEventByRef(a)
    MsgBox a.Text
End Sub

// CTestObject.cls

Option Explicit

Public Text As String

Here is a repro in a zip

https://axolotl.blob.core.windows.net/issue2863/Issue2863.zip

you have to first register the .ocx “regsvr32 Issue2863\VB6OCX\Axolotl.TestControl.ocx”

then run ConsoleApp1 (core) in debug with exceptions turned on… (ConsoleApp2 is framework and works)

Issue2863\VB6OCX\Axolotl.TestControl.ctl

MsgBox a.Text <== a is corrupt

Exception thrown at 0x11001F16 (Axolotl.TestControl.ocx) in ConsoleApp1.exe: 0xC0000005: Access violation reading location 0x00000000. occurred

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
NikolaosWakemcommented, Jun 26, 2020

3.1 LTS broken but fixed in 5.0 so all Good 😄

0reactions
msftbot[bot]commented, Nov 29, 2022

This submission has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 14 days.

It will be closed if no further activity occurs within 7 days of this comment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Application crash when using .ocx with events that have ref ...
Application crash when using .ocx with events that have ref objects, works in Framework - System.Windows.Forms.AxHost · dotnet/winforms@0b4e37a.
Read more >
Windows Forms application crash without any message
Hello, I have developed a windows forms app to copy document from web repository to another one. The application works fine but after...
Read more >
c# - COMException while instantiating a form containing an ...
NOTE: The identical Form in WinForms application causes no problems. Removing the ActiveX control from this Form removes the exception (in WPF).
Read more >
Problems using on old OCX ActiveX control in Visual ...
I am using Windows 10 desktop. I have both Visual Studio 2012 and 2022 installed. I have an activeX control, that I can...
Read more >
Dynamically adding ActiveX controls in managed code
A technique for dynamically adding ActiveX controls to managed code. ... Windows.Forms.AxHost . Regardless of whether you are using C# or VB ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found