Re: How To Pass A CStringArray From Application To DLL?
Originally Posted by saraswathisrinath
My doubt is, CStringArray items are allocated in the DLL. So, when i release the CStringArray object in the application.
Is it can release memory that are allocated in the DLL ?
You shouldn't pass objects such as CStringArray across application boundaries. There are two main reasons:
1) Can you guarantee that the object has exactly the same binary layout in the application as the DLL? What if you tried to pass a CStringArray from a VS 2008 application to a DLL created with VS 2010? Or if the CStringArray in the application was compiled with different options than the one in the DLL?
2) Can you guarantee that the object uses the same memory heap when it needs to allocate/deallocate memory for itself? That is exactly what the error is telling you -- you are using a CStringArray created that uses one heap, and then you're taking the same CStringArray and attempting to use the other heap on it.
These items not only pertain to CStringArray, any object, whether it is MFC, ATL, STL, or your own home-made object shouldn't be passed between application boundaries. Even simple C structs such as FILE shouldn't be passed across app boundaries because of 1) above.
The only way to successfully pass a CStringArray between an app and a DLL is to
1) Guarantee that the application and DLL were created with the same version of the compiler (same version, same service pack, etc.) as well as compiled with the same compiler options, same packing options, etc.
2) Guarantee that the application and DLL use the same heap by using the same Multithreaded DLL runtime.
Given these restriction, you may be better off redesigning your code so that you're not passing the CStringArray -- instead you create exported functions that manipulate the CStringArray inside the DLL.
Anyway, C++ classes are designed to be used internally inside each application (I am talking about the end-programmer, and not necessarily a component library designer). Passing these objects between application and DLL as if you're passing them between functions is not what C++ objects are really designed for.
Last edited by Paul McKenzie; February 11th, 2013 at 04:57 AM.