Never use New to create MTS/COM+ objects

Never use New to create MTS/COM+ objects

A common question among VB developers is: Why is it dangerous the use of the New keyword in VB to create COM objects registered under MTS/COM+?

The New keyword is the only way to go when you want to create objects that are defined as PublicNotCreatable or Private (CreateObject doesn’t work) inside the same component as the calling code. As you know VB lets you use New to create Multiuse classes as well. Unfortunately the New keyword has a couple of drawbacks:

1) In situations where the caller and the callee are in the same EXE/DLL COM component VB perform an internal creation of the COM object bypassing the COM run-time (and the MTS run-time if the object is registered under MTS). While this fact is not a problem under a non-MTS environment, the situation gets very dangerous under MTS and COM+. What happens is that, since MTS/COM+ is not notified of the object creation, the newly created object gets access to the ObjectContext of the father and MTS sees the code running inside method calls of the child object as “inline-code” of the father. I leave as an exercise to the reader what can happen if the child object calls SetComplete (against the ObjectContext of the father).

2) When you declare an object (Interface) with the syntax

Dim x As New MyClass

VB checks every time the object is referenced in the code. If it is Nothing, VB silently (re)creates it (This means you’ll never get a “object variable or with block not set” error). If you are not aware of this your code could follow unexpected execution paths.

 

Public sub DoWork()        Dim X as New MyClass   ' MyClass is registered as "requires transaction"  x.dosomemorework       ' VB creates it, the component ball start spinning in                          ' the MTS explorer, the transaction start  Set x = Nothing        ' The ball stop spinning  If x Is Nothing Then   ' VB recreates it, the ball start spinning again, the                          ' transaction start again      Format Drive C     '  this code will never be executed  End If       ' From here to when x goes out of scope x is activated   ' (and probably you don't know this).  End Sub    
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes