Sign in to follow this  
JohnnyCode

malloc/free across modules

Recommended Posts

Memory allocated by a particular C/C++ runtime should be deallocated by the same runtime, because different runtimes may use separate or incompatible allocation structures. This is not an issue if the two modules use the same runtime library version and link to it dynamically, which is the most common situation if you aren't dealing with third-party plugins and haven't changed any default subsystem settings. But yes, this is an issue with both new/delete and malloc/free.

Share this post


Link to post
Share on other sites
I think the question here is, "what do you mean by module?"

If, by module, you mean a particular class or group of function, the the 1st response is what your looking for.

If, by module, you mean different libraries or plugins, then the 2nd response is what you're looking for.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnnyCode
I wonder wheather this aply only for new/delete, or for malloc/free as well.


All of these may be redefined. In practice, they will be a regular function that performs some syscall to obtain memory. Redefinition can also occur indirectly, due to linking or other obscurities. Often it will be a bug, an undesirable side-effect of linking but sometimes it'll be intentional or required.

All that matters is that correct pairs are used. Same new and delete and same malloc and free.

What same means is rather vague, but different versions of new could use different memory pools. So allocating from pool A and freeing into pool B is obviously an error. In OO terms, new/free would be member functions, and same instance would need to be used.

This is a fairly big deal when integrating third-party libraries. Same type of issues occurs when passing STL allocators across DLL boundaries.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this