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:
- Created 4 years ago
- Comments:7 (4 by maintainers)

Top Related StackOverflow Question
3.1 LTS broken but fixed in 5.0 so all Good 😄
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.