How To Pass A CStringArray From Application To DLL?
I passed the CStringArray to dll & wrote the String in a file.
The error File: array_s.cpp, Line No: 420 was occured when i close the .exe file.
For your reference:
Function Declaration in DLL :
DECLDIR void SetParName(CStringArray& ParName);
Function Definition in DLL :
DECLDIR void SetParName(CStringArray& ParName)
Calling Function in Application :
_SetParNameFunc(ParName); //Function Called Using CStringArray
My doubt is, CStringArray items are allocated in the DLL. So, when i release the CStringArray object in the application.
File : array_s.cpp
Line : 420
void CStringArray::AssertValid() const
if (m_pData == NULL)
ASSERT(m_nSize == 0);
ASSERT(m_nMaxSize == 0);
ASSERT(m_nSize >= 0);
ASSERT(m_nMaxSize >= 0);
ASSERT(m_nSize <= m_nMaxSize);
ASSERT(AfxIsValidAddress(m_pData, m_nMaxSize * sizeof(CString))); //Line : 420
Is it can release memory that are allocated in the DLL ?
Re: How To Pass A CStringArray From Application To DLL?
You shouldn't pass objects such as CStringArray across application boundaries. There are two main reasons:
Originally Posted by saraswathisrinath
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.