Is there any problem to allocate memory in an application and free it in a function called from a dll/dylib?
But to allocate memory in a function from a dll/dylib and free it in the caller application?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For a static library it is not generally a problem, but is usually not a good idea for dynamic libraries. Especially for libraries shared between projects.
The problem is you need to ensure that the memory allocation functions (new/delete, malloc/free) match exactly between your calling code and the dynamic library. For example, if you statically link the C-runtime with your executable, but the dynamic library is dynamically linked (or vice versa) then you have separate code running malloc for the executable vs the dynamic library.
To avoid any problems dynamic libraries will often expose their own alloc and free routines to guarantee consistency.