The simplest COM Interop app leaking memory - What I do wrong? - com-interop

C#
namespace TestController
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IController {
void DoSomething();
}
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
public class ControllerImpl : IController {
public void DoSomething()
{
throw new NotImplementedException();
}
}
}
C++
#import "c:\prj\Controller\bin\Debug\TestController.tlb"
using namespace TestController;
int _tmain(int argc, _TCHAR* argv[])
{
IControllerPtr ctrl;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
while (true) {
HRESULT hr = ctrl.CreateInstance(__uuidof(ControllerImpl));
ctrl = 0;
}
return 0;
}
Hi all,
I need to provide access to my .NET class library from unmanaged code. Beeing totally new to the subject, I spent several days studying the COM / interop, then defined and implemented a COM accessible interface, made a test run and everything just worked, until I noticed something that seemed as a memory leak. I isolated the offending statements, but still have no clue why is the above code broken.

This is probably a case of:
http://jpassing.com/2009/03/26/rcw-reference-counting-rules-com-reference-counting-rules/
Specifically, .NET uses RCW reference counting rules, which are different to the usual reference counting rules in COM applications.
This results in apparent leaks until the .NET RCW reference counting observes that a given object is no-longer used, and this requires cooperation from its garbage collector.

Related

Casting an object with events defined to an interface type causes an internal compiler error

I've got an interface with a simple signature:
namespace Serial {
public interface struct ISerial
{
uint16_t func1();
uint16_t func2();
};
}
and then a class type which implements the interface
namespace Serial {
public delegate void MyEventClass();
public ref class MySerial sealed : public ISerial {
public:
event MyEventClass MyEvent;
MySerial();
...
};
}
but elsewhere, as a default parameter to a function, I try to store a reference to a type MySerial as an ISerial ^
void
begin(
Serial::ISerial ^s = ref new Serial::MySerial
);
causes: error C1001: An internal error has occurred in the compiler.
when I remove the event from the class definition, everything compiles fine. I'm finding little information on this error.
I verified this on VS 2013 and it works with a few minor changes (all generated based on normal compiler errors, not an ICE). I don't have VS 2015 available right now, but will log a bug if it still repros.
First the struct (should be unchanged)
namespace Serial
{
public interface struct ISerial
{
uint16_t func1();
uint16_t func2();
};
}
Then the class (couple of changes noted below):
namespace Serial
{
public delegate void MyEventClass();
public ref class MySerial sealed : public ISerial{
public:
event MyEventClass^ MyEvent;
MySerial(){}
virtual uint16_t func1() { return 42; }
virtual uint16_t func2() { return 42; }
};
}
And the usage:
void foo()
{
using namespace Serial;
ISerial^ foo = ref new MySerial();
}
Basically you need to add the hat (^) to the event type, and you need to add virtual to the methods (but do not add override).
See more here on MSDN

c++ cli interface event explicit implementation

I am trying to convert c# code into c++/cli. Everything went smoothly until i started translating interface event explicit implementations into c++/cli syntax.
Let's say in c# i have this interface
public interface Interface
{
public event MyEventHandler Event;
}
Which is implemented in Class in explicit way, so it doesn't conflict with another member by its name:
public interface Class : Interface
{
event MyEventHandler Interface.Event;
public event AnotherEventHandler Event;
}
I am trying to convert Class into c++/cli as follows:
public ref class Class : public Interface
{
virtual event MyEventHandler^ Event2 = Interface::Event
{
}
...
};
This won't compile giving me syntax error in "... = Interface::Event" part. Does anyone have idea what is the right syntax, or does it even exist in c++/cli? I spent some time searching over the Internet, but failed to bump into anything useful.
UPDATE: Here is complete c++/cli code that demonstrates the problem:
public delegate void MyEventHandle();
public delegate void AnotherEventHandle();
public interface class Interface
{
event MyEventHandler^ Event;
};
public ref class Class : public Interface
{
public:
virtual event MyEventHandler^ Event2 = Interface::Event
{
virtual void add(MyEventHandle^) {}
virtual void remove(MyEventHandle^) {}
}
event AnotherEventHandler^ Event;
};
The error output by VC++ 2012 is "error C2146: syntax error : missing ';' before identifier 'MyEventHandler'"
You have to make it look like this:
event MyEventHandler^ Event2 {
virtual void add(MyEventHandler^ handler) = Interface::Event::add {
backingDelegate += handler;
}
virtual void remove(MyEventHandler^ handler) = Interface::Event::remove {
backingDelegate -= handler;
}
};

Entity framework asp.net MVC3 .NET 4

I Have a little snag. With my asp.net mvc3 application.
When I trying to compile my app I obtain this Error
Error 2 'Blog.Domain.Concrete.EFEntryRepository' does not implement interface member 'Blog.Domain.Abstract.IEntryRepository.SaveEntry(Blog.Domain.Entities.Entry)' D:\dokumenty\Visual Studio 2010\Projects\MVC3\Blog\Blog.Domain\Concrete\EFEntryRepository.cs 10 19 Blog.Domain
This is my Interface.
namespace Blog.Domain.Abstract
{
public interface IEntryRepository
{
IQueryable<Entry> Entries { get; }
void SaveEntry(Entry entry);
void DeleteEntry(Entry entry);
}
}
And this is my implementation of it.
public class EFEntryRepository : IEntryRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Entry> Entries
{
get { return context.Entries; }
}
public void SaveEntry(Entry entry)
{
if (entry.EntryID == 0)
context.Entries.Add(entry);
context.SaveChanges();
}
public void DeleteEntry(Entry entry)
{
context.Entries.Remove(entry);
context.SaveChanges();
}
}
This is link to my project. http://sigma.ug.edu.pl/~kkubacki/Blog.zip //NEW
Now I is compiling.
What I Do Wrong ?
I have new Information about the bug. Now the solution is compiling but the application crashes with the bug information
"{"The type 'Blog.Domain.Concrete.Entry' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject."} " Visual studio shows bug in the EFEntryRepository class.
I don't know what to do please help.
OK Problem IS Solved.
You have two different Entry classes in different namespaces.
Blog.Domain.Entities.Entry
Blog.Domain.Concrete.Entry
The interface is referring to one and the implementation is referring to the other.

C++/CLI Generic::Dictionary Declaration Syntax

I'm been curious about the declaration syntax of Collections::Generic::Dictionary
class in C++/CLI.
Normally we declare a reference in a class and initialize it:
public ref class CDemo {
private: ClassA ^ m_InstanceA;
// Why the absence of '^'.
private: Dictionary<int, int> m_Dic;
CDemo() :
m_InstanceA(gcnew ClassA()),
m_Dic(gcnew Dictionary<int, int>())
{...}
};
Could someone explains please why should the '^' absent there?
What's more, if I were to use the dictionary above as a TValue of another dictionary,
I have to declare it like this:
Dictionary<T, Dictionary<T, T>^ > m_Dic; // A '^' in the TValue parameter, which is
// normal, but same question as above,
// I don't have to declare m_Dic as ^ ?
Thanks.
This is not specific to Dictionary. This syntax is a way to help map C++ semantics onto managed types. In general:
ref class A
{
ReferenceType m_obj;
};
is roughly equivalent to
class A : IDisposable
{
private ReferenceType m_obj;
void Dispose() { m_obj.Dispose(); }
}
in C# if ReferenceType implements IDisposable. It is perfectly possible to write
ref class A
{
ReferenceType^ m_obj;
};
This does not have the implicit IDisposable support. The other difference is that you can return a ReferenceType^ from a method, this is not supported with just plain ReferenceType. For example:
ref class A
{
ReferenceType^ m_obj;
ReferenceType^ GetIt() { return m_obj; }
};
will compile,
ref class A
{
ReferenceType m_obj;
ReferenceType GetIt() { return m_obj; } // won't compile
ReferenceType^ OtherGetIt() { return m_obj; } // neither will this
};
A similar distinction is provided for automatic (stack variables)
ReferenceType local;
local.Stuff();
is desugared by the compiler to
try {
ReferenceType^ local = gcnew ReferenceType();
local->Stuff();
} finally {
delete local; // invokes Dispose() (~ReferenceType)
}
These features bring the familiar idiom of RAII to C++/CLI with managed types.
EDIT:
Yes, the Dispose method of IDisposable is analogous to a C++ destructor. If ReferenceType doesn't implement IDisposable (doesn't have a dtor), and it is the only member, A will also not implement IDisposable (not have an implicit dtor). In C++/CLI you implement IDisposable by providing a dtor (for managed types).

Visual Studio code generated when choosing to explicitly implement interface

Sorry for the vague title, but I'm not sure what this is called.
Say I add IDisposable to my class, Visual Studio can create the method stub for me. But it creates the stub like:
void IDisposable.Dispose()
I don't follow what this syntax is doing. Why do it like this instead of public void Dispose()?
And with the first syntax, I couldn't work out how to call Dispose() from within my class (in my destructor).
When you implement an interface member explicitly, which is what the generated code is doing, you can't access the member through the class instance. Instead you have to call it through an instance of the interface. For example:
class MyClass : IDisposable
{
void IDisposable.Dispose()
{
// Do Stuff
}
~MyClass()
{
IDisposable me = (IDisposable)this;
me.Dispose();
}
}
This enables you to implement two interfaces with a member of the same name and explicitly call either member independently.
interface IExplict1
{
string InterfaceName();
}
interface IExplict2
{
string InterfaceName();
}
class MyClass : IExplict1, IExplict2
{
string IExplict1.InterfaceName()
{
return "IExplicit1";
}
string IExplict2.InterfaceName()
{
return "IExplicit2";
}
}
public static void Main()
{
MyClass myInstance = new MyClass();
Console.WriteLine( ((IExplcit1)myInstance).InstanceName() ); // outputs "IExplicit1"
IExplicit2 myExplicit2Instance = (IExplicit2)myInstance;
Console.WriteLine( myExplicit2Instance.InstanceName() ); // outputs "IExplicit2"
}
Visual studio gives you two options:
Implement
Implement explicit
You normally choose the first one (non-explicit): which gives you the behaviour you want.
The "explicit" option is useful if you inherit the same method from two different interfaces, i.e multiple inheritance (which isn't usually).
Members of an interface type are always public. Which requires their method implementation to be public as well. This doesn't compile for example:
interface IFoo { void Bar(); }
class Baz : IFoo {
private void Bar() { } // CS0737
}
Explicit interface implementation provides a syntax that allows the method to be private:
class Baz : IFoo {
void IFoo.Bar() { } // No error
}
A classic use for this is to hide the implementation of a base interface type. IEnumerable<> would be a very good example:
class Baz : IEnumerable<Foo> {
public IEnumerator<Foo> GetEnumerator() {}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { }
}
Note how the generic version is accessible, the non-generic version is hidden. That both discourages its use and avoids a compile error because of a duplicate method.
In your case, implementing Dispose() explicitly is wrong. You wrote Dispose() to allow the client code to call it, forcing it to cast to IDisposable to make the call doesn't make sense.
Also, calling Dispose() from a finalizer is a code smell. The standard pattern is to add a protected Dispose(bool disposing) method to your class.

Resources