I have implemented a COM component in C#:
[ComVisible(true)]
[System.Runtime.InteropServices.Guid("E052BB1C-7ADC-47F4-99E1-9407E2FA0AA2")]
public interface IColorRamps
{
IColorRamp getColorRamp();
}
[ComVisible(true)]
[Guid("EE47F2F2-0AD9-437C-8815-D570EACF2C07")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ColorRamps.ColorRamps")]
public class ColorRamps : IColorRamps
{
public IColorRamp getColorRamp() { ... }
}
I call this from C++:
IColorRampPtr colorRamp;
{
ColorRamps::IColorRampsPtr colorRamps(ColorRamps::CLSID_ColorRamps);
HRESULT hr = colorRamps->getColorRamp(&colorRamp);
colorRamp.AddRef(); // Should I do this??
}
At first I did not have the AddRef() call and things seemed to work except I got strange crashes on "R6025 (pure virtual function call) run-time error" after running this code many times.
The signature in the autogenerated .tlh file is:
virtual HRESULT __stdcall getColorRamp(/*[out,retval]*/ struct IColorRamp * * pRetVal ) = 0;
When calling functions like this in C++ I am used to the function doing an AddRef() itself and passing on memory ownership to the caller. Is this not the case in C# COM?
I did NOT call Marshal.AddRef() inside ColorRamps.getColorRamp().