断言:指针必须来自“本地”堆 [英] Assertion: The pointer MUST come from the 'local' heap

查看:188
本文介绍了断言:指针必须来自“本地”堆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个名为clunk的音效库( http://sourceforge.net/projects/clunk/ )。
我为visual studio 11构建了这个库,并将它链接到我的视觉工作室项目中。当我尝试test.cpp我得到一个断言由msvcr110d.dll抛出。



它是否与我的运行时库管理设置有关:它是多线程调试DLL(/ MDd)
在clunk中的cmakelist.txt中,我添加了以下代码行:

  set(CMAKE_CXX_FLAGS_DEBUG$ {CMAKE_CXX_FLAGS_DEBUG} / MDd)



我仍​​然收到指针分配有问题的消息。为什么?

解决方案

你可能在应用程序/库边界的一侧分配内存,这很难正确,最好避免。



您必须确保内存返回到分配它的同一个分配器。这里有几个模式来避免这个问题:


  1. 而不是为返回的结构分配内存的库,让应用程序做它。


  2. 让库为结构分配内存,而不是释放应用程序,让应用程序调用一个特殊的功能。因此,如果库中有一个'getFoo'函数返回一个分配的结构,有一个'freeFoo'函数释放该结构。


  3. 让库使用静态分配的结构,直到某个特定的下一个调用到库。 / p>


  4. 向库提供一个setAlloctor函数,并将一个指针传递给 malloc free 。这样,库将始终使用应用程序的分配器。


  5. 给库一个 getAllocator 返回指向库正在使用的 malloc 免费函数的指针。这样,应用程序可以从库的allocator中获取内存(对于库而言可能是免费的),或者将内存返回到库的allocator(分配的库)。


看看生成断言的代码,看看是否可以修改它来使用这些模式之一。例如,您可以在从库中获取的对象的指针上调用 delete ,当您应该使用库提供的特殊析构函数时。


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?

解决方案

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.

这篇关于断言:指针必须来自“本地”堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆