Assertion: The pointer MUST come from the 'local' heap

给你一囗甜甜゛ 提交于 2019-12-13 04:16:50

问题


I am testing a little sound library called clunk (http://sourceforge.net/projects/clunk/). I built that library for visual studio 11 and linked it in my visual studio project. When I try the test.cpp I am getting an assertion thrown by msvcr110d.dll.

Does it have to do with my runtime librarie settings: It is "Multithreaded-Debug-DLL (/MDd)" ? In cmakelist.txt in clunk I added following line of code:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")

I am still getting the message that there are problems with pointer allocation. Why that?


回答1:


You're probably allocating memory on one side of an application/library boundary and freeing it on the other. That's difficult to get right and likely best avoided.

You must ensure that memory is returned to the very same allocator that allocated it. Here are a few patterns to avoid this problem:

  1. Instead of the library allocating memory for a returned structure, have the application do it. Then the application can free the structure.

  2. Let the library allocate memory for a structure, but instead of the application freeing it, have the application call a special free function. So if there's a 'getFoo' function in the library that returns an allocated structure, have a 'freeFoo' function that releases that structure. This ensures the library returns the structure to its own allocator.

  3. Have the library use statically allocated structures that are valid until some particular next call into the library.

  4. Give the library a 'setAlloctor' function and pass it a pointer to malloc and free from the application. This way, the library will always use the application's allocator.

  5. Give the library a getAllocator function that returns pointers to the malloc and free functions the library is using. This way, the application can get memory from the library's allocator (for the library to possibly free) or return memory to the library's allocator (that the library allocated).

Take a look at the code that's generating the assertion and see if it can be modified to use one of these patterns. It's possible, for example, that you're just calling delete on a pointer to an object you got from the library when you should be using a special destructor function provided by the library.



来源:https://stackoverflow.com/questions/14893649/assertion-the-pointer-must-come-from-the-local-heap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!