Code in C#
string GetValue()
{
//m_IFC is RCW of a native COM object returns Variant marshalled as managed object
object value1 = m_IFC.GetValue();
string valueStr = (string) value1;
return valueStr;
}
The assembly having above code is hosted in COM out-of-proc server which returns the received string from the API to all clients.
Are there any any potential memory leaks here?
From the code you show there is no potential memory leak if m_IFC is indeed a standard runtime callable wrapper. From the MSDN documetation:
The standard wrapper enforces built-in marshaling rules. For example, when a .NET client passes a String type as part of an argument to an unmanaged object, the wrapper converts the string to a BSTR type. Should the COM object return a BSTR to its managed caller, the caller receives a String. Both the client and the server send and receive data that is familiar to them.
All objects shown in your code are managed types and will be garbage collected when all references to it go out of scope. Any unmanaged memory is already handled by the wrapper.
Related
I have below Extension Method. I am passing all values as parameters.I would like to know whether it is a thread safe method if this method accessible by the multiple threads.
public static string ConcatLogKeyWithExceptionMessage<T>(this T entity, string configuredLogKeys, bool logOnlySingleKey, string exceptionMessage, bool firstInvocation = true, StringBuilder logKeyBuilder = null, string[] configuredKeysArray = null) where T : class
{
}
If you need more info please let me know
Thread safety is a concern about how the data behaves when multiple threads access it at the same time. When is said that a method is thread safe, it means that either the method content is caring of synchronizing access to non-thread safe data structures; the data structures manipulated with the method are inherently thread safe; or both.
If your method is thread safe or not, will depend on if for example the members of T entity that you are manipulating are thread safe or not. The same for the StringBuilder, the array, and anything that is passed by reference. If that method is a pure function you could say it is thread safe. However, if that method is causing side effects in any of the parameters, it would depend on how those changes are performed.
According to JavaDoc of Object.hashCode() this method returns internal address of the object converting it to integer value.
But Garbage Collector can move the object from one memory segment to another changing it internal address. For example the object can be moved from Young Generation to Old Generation.
See for example the following command line keys of java.exe:
-XX:+UseSerialGC
-XX:+UseParallelGC
-XX:+UseParallelOldGC
-XX:+UseConcMarkSweepGC
They determine different algorithms of Garbage Collection.
Does it mean that in this case Object.hashCode() will return different values?
Or it will always return value corresponding to initial address of the object?
Straight from the javadoc
The general contract of hashCode is:
•Whenever it is invoked on the same object more than once during an
execution of a Java application, the hashCode method must consistently
return the same integer, provided no information used in equals
comparisons on the object is modified. This integer need not remain
consistent from one execution of an application to another execution
of the same application.
I want to know the actual usage of QueryInterface and IUnknown interface.
QueryInterface() is a COM version of C# as keyword - you call QueryInterface() and supply an interface id and you either get success code (S_OK) and a valid pointer to that interface of the object or error code E_NOINTERFACE and null pointer which means the object doesn't implement such interface. IUnknown is the interface containing QueryInterface() and also the reference counting methods (AddRef() and Release()) which are used for COM object lifetime management. Every COM object must implement at least IUnknown, otherwise you simply can't Release() objects when you no longer need them and calling Release() is the only way to tell you no longer need the object.
QueryInterface checks whether the object that implements this interface supports the interface specified by IID. If so, QueryInterface
Increments the reference count.
Sets the Obj parameter so that it
points to an instance of the specified interface.
Returns 0 to indicate success.
If the object does not support the interface, QueryInterface returns a nonzero error code, such as E_NoInterface.
IUnknown is the fundamental interface in COM-Lite, as in COM. All other COM-Lite interfaces must derive from it.
Used for Object lifetime management (when to free an object) and object self-description (how to determine object capabilities at runtime)
In Java I create some Threads, and they call the same native method like:
public native String go(String str);
In C Language, I have to make an object for each Thread, and the object is only used by each Thread.
Global reference cannot be used because it's shared by all Threads.
I don't want to create the object each time I invoke the JNI method.
How can I implement this?
So you want thread-local Java objects at the JNI side? Just create a ThreadLocal and store it in a global reference.
What are the differences between creating an instance of a .NET object in C++ that is managed vs. unmanaged. That is to say, what are the differences between these to statements:
StreamWriter ^stream = gcnew StreamWriter(fileName);
versus
StreamWriter *stream = new StreamWriter(fileName);
My assumption is that if I use gcnew, the memory allocated for StreamWriter will be managed by the garbage collector. Alternatively if I use the pointer(*) and new keyword, I will have to call delete to deallocate the memory.
My real question is: will the garbage collector manage the memory that is being allocated inside of the .NET objects? For instance if a .NET object instantiates another object, and it goes out of scope - will the garbage collector manage that memory even if I use the pointer(*) and new keyword and NOT the gcnew and handle(^).
In C++/CLI, you can't new a .NET object, you'll get something similar to the following error:
error C2750: 'System::Object' : cannot use 'new' on the reference type; use 'gcnew' instead
Usage of new for .NET objects is allowed in the older Managed Extensions for C++ (/clr:oldsyntax compiler flag). "Managed C++" is now deprecated because it is horrible. It has been superceded by C++/CLI, which introduced the ^ and gcnew.
In C++/CLI, you must use gcnew (and ^ handles) for managed types and you must use new (and * pointers) for native types. If you do create objects on the native heap using new, it is your responsibility to destroy them when you are done with them.
Ideally you should use a smart pointer (like std::shared_ptr or std::unique_ptr) to manage the object on the native heap. However, since you can't have a native smart pointer as a field of a ref class, this isn't entirely straightforward. The simplest and most generic approach would probably be to write your own smart pointer wrapper ref class that correctly implements IDisposable.
When you create an object with gcnew it is binded to the garbage collector, and the garbage collector will be responsible for destroying it.
If you use new it won't be binded to the garbage collector, and it will be your responsibility to delete the object.
Just to clarify:
If you have a managed C# object that contains within it an unmanaged object, the garbage collector will not delete the unmanaged object. It will just call the managed object's destructor (if it exists) before it is deleted. You should write your own code in the destructor to delete the unmanaged objects you created.