- Simply put, COM is a way of building objects that is
independent of any programming language.

2. Why do we need COM ?

- COM allows reuse of objects with no
knowledge of their internal implementation

- if software component has interface standard, any program
can put interface to use it.

3. What is Benefit of COM ?

- It is used to enable
interprocess communication and dynamic object creation in a large range of
programming languages.

4. What is Component ?

- when separation of a single piece of software in the
abstract conceptual

- a number of COM objects

- compiled binary

5. What is Benefit of Component

- Application Customizing, Easy to change of application

6. What are Client and Server in COM

- Client : Process which uses component

- Server : exe file or DLL is implemented as a Component

COM Interface

1. What is interface

- each software components that support COM take advantage
of the capabilities through interface

- It is not a standard at the level of source code, but it
is a standard at the level of binary level.

- Interface is virtual function table

Ex)

struct IExample

{

virtual void FunctionA() = 0;

virtual void FunctionB() = 0;
}

- Interface is represented by attaching 'I' followed by the
name of

- Interface is declared just by cover, doesn’t implement the
contents of the function. The role of the interface isn’t
implementation some features, that can be shared among different software
components that define the shape is because of features.

- In other words, the interface has a
different software components, functions as a link that allows you to share.
Let’s do something in between calling and called to set an appointment.

- Microsoft has predefined more than 100 interfaces that any
COM object can support. These interfaces are called standard interfaces.
User-defined interfaces such as IMath and ISpelling are custom interfaces. COM
objects can use standard interfaces, custom interfaces, or a combination of the
two.

- One of the rules of COM says that
given a pointer to an interface, a client can call any IUnknown method through
that pointer as well as any methods that are specific to the interface. In
other words, all interfaces must support the three IUnknown methods in addition
to their own methods. This means that if you define an IMath interface with
methods named Add and Subtract, the interface actually contains five methods:
QueryInterface, AddRef, Release, Add, and Subtract. Most objects don't
implement IUnknown as a separate interface. Because all interfaces include the
IUnknown methods, most objects, if asked for an IUnknown pointer, simply return
a pointer to one of their other interfaces.

The pic shows a schematic of a simple COM
object. The sticks, or "lollipops" as they're sometimes called,
represent the object's interfaces. The IUnknown lollipop is often
omitted because it's understood that every COM object implements IUnknown.

The reflexive
property refers to the ability for the QueryInterface() call on a given
interface with the interface's ID to return the same instance of the
interface.

The symmetric property

The symmetric
property requires that when interface B is retrieved from interface A via
QueryInterface()

The transitive property

The
transitive property requires that if interface B is obtained from interface A
and interface C is obtainable from interface B, then interface C should be
retrievablefrom interface A

- Object Lifetimes

C++ programmers
are used to creating heap-based objects using the C++ new operator. They're
also accustomed to calling delete to delete the objects that they create with new.
COM differs from C++ in this respect, because clients create object instances
but they don't delete them. Instead, COM objects delete themselves. Here's why.

Suppose two or
more clients are using the same instance of an object. Client A creates the object,
and Client B attaches to the object by somehow acquiring an interface pointer.
If Client A, unaware that Client B exists, deletes the object, Client B is left
with an interface pointer that no longer points to anything. Because a COM
client typically doesn't know (and doesn't care) whether it's the sole user of
an object or one of many users, COM leaves it up to the object to delete
itself. Deletion occurs when an internal reference count maintained by the
object drops to 0. The reference count is a running count of the number of
clients holding pointers to the object's interfaces.

For COM classes
implemented in C++, the reference count is typically stored in a member
variable. The count is incremented when AddRef is called and decremented when
Release is called. (Remember that because AddRef and Release are IUnknown
methods, they can be called through any interface pointer.) Implementations of
AddRef and Release are normally no more complicated than this:

CoClass

- One or more of the interface and the
interface code that refers to the actual implementation. Either alone or together many more are stored in the form
of a DLL or EXE file

- CoClass
methods

- AddRef() : increase 1 of
reference counter

- Release() : decrease 1 of
reference counter

- QueryInterface() : get interface’s
pointer. QueryInterface must have stable behavior: If it succeeds once for a
particular interface on a particular instance, it should always succeed

- User-Defined method : Implement thefunction

- COM Object : CoClass is created in
memory after instantiation

- COM Server : one or more of theCoClassis created and DLL orEXEfilethat is stored in theunit

- COM Client :Software which using COM server’s functions

- In other to exec Program, have to know the file name or
ID. CoClass use GUID instead of using file name. GUID is Identifiers
and is called it IID for short. CLSID is CoClass’s Identifiers.

-HRESULT : return value type of
method which is used in COM, An HRESULT tells the caller whether a call
succeeded or failed

- COM Library : COM is supported by the
operating system from a series of related services

The following HRESULT values are the
most common. More values are contained in the header file Winerror.h.

What is a memory leak in C++ ?

A memory leak occurs when a piece (or pieces) of memory that was previously allocated by a programmer is not properly deallocated by the programmer.
Even though that memory is no longer in use by the program, it is still “reserved”, and that piece of memory can not be used by the program until it is properly deallocated by the programmer.
That’s why it’s called a memory leak – because it’s like a leaky faucet in which water is being wasted, only in this case it’s computer memory.

What problems can be caused by memory leaks?

The problem caused by a memory leak is that it leaves chunk(s) of memory unavailable for use by the programmer.
If a program has a lot of memory that hasn’t been deallocated, then that could really slow down the performance of the program.
If there’s no memory left in the program because of memory leaks, then that could of course cause the program to crash.

What is the difference between delete and delete[ ]?

The difference is big, and very important to understand.
Whenever you create an object in C++ using the new keyword you will have to delete it.
Whether you use “delete” or “delete[ ]” really depends on what kind of object you are creating.

This is best illustrated by an example:

void foo ( )
{
string *sp = new string[50];
string *s = new string;
delete s;
// have to use "[ ]" since sp points to an array of strings:
delete[ ] sp;
}
we had to use the “[ ]” when deleting the memory that “sp” points to, because the sp pointer points to an array of strings.
So, in order to properly remove memory created with a “[ ]“, we have to use the “delete [ ] “.

Case1) An example of a memory leak in C++

(Pointer goes out of scope) Here is an example of a memory leak in C++:

void foo ()
{
int *i = new int;
*i = 44;
}

OR

void foo()
{
char* str = new char[20];
strcpy(str,"memory leak");
}

So, the problem with the code above is that the “*data” pointer is never deleted – which means that the data it references is never deallocated, and memory is wasted.

Case2) An example of a memory leak in C++

(Wrong usage of new/delete) Here is an example of a memory leak in C++:

Case6) An example of a memory leak in C++

("Lost" pointers) Here is an example of a memory leak in C++:

void foo()
{
char* str = new char [30]; // Give str a memory address.
// delete [] str; // Remove the first comment marking in this line to correct.
str = new char [60]; // Give str another memory address with the first one gone forever.
delete [] str; // This deletes the 60 bytes, not just the first 30.
}