Category: Proxy usage
Why am I getting the following compiler error:
"C2440: 'initializing' : cannot convert from 'java::lang::String *' to 'java::lang::String'"
You made the fairly common mistake of using your Java habits in C++. You probably wrote something like:
String temp = new String( "test" );
What you should have written instead is:
String temp = String( "test" );
or even simpler:
String temp = "test";
The problem with the original snippet is that you are allocating a String instance on the C++ heap using operator new (). Then you're assigning the resulting pointer to a non-pointer type variable.
If you really want to allocate a proxy instance on the C++ heap all you have to do is declare the variable holding the instance as a pointer type:
String * temp = new String( "test" );
Once you allocate a proxy instance on the C++ heap, you are totally responsible for deleting it! IF you don't, you have both a C++ memory leak and a Java object leak!
You rarely have to allocate proxies on the heap. The entire runtime framework is designed to allow you to avoid C++ memory management issues by using "automatic" objects. The only times when you might have to use pointers are when you
- declare global objects (to be avoided anyway) and need to prevent the JVM to be loaded when the C++ runtime initializes the object.
- need to extend the lifecycle of an object beyond its natural scope.
