简体   繁体   English

释放指针没有分配所有对象的问题!

[英]Pointer being freed was not allocated problem with ALL objects!

I'm working (at least trying...) on a Cocoa application, which uses several custom Cocoa frameworks and one of these frameworks is a mix of C++ and Objective C++ (mostly C++ ) code... The problem is that each memory deallocation inside this framework gives me the Pointer being freed was not allocated error, even in the following trivial case: 我正在(至少尝试...)在一个Cocoa应用程序上工作,该应用程序使用了几个自定义Cocoa框架 ,而这些框架之一是C ++Objective C ++ (主要是C ++ )代码的混合...问题是每个内存在此框架内的释放使我无法释放指针,但未分配错误,即使在以下情况下也是如此:

class testClass
{
public:
    testClass() { }
    virtual ~testClass() { }
};

void test()
{
    testClass *p = new testClass();
    delete p;
    // malloc: *** error for object 0x2800510: pointer being freed was not allocated
    //*** set a breakpoint in malloc_error_break to debug
    p = NULL;
}


malloc_error_break tells me that the object's destructor is being called , however every next allocation increases the object's address, so the memory is really not being freed... Please tell me, WHY?! malloc_error_break告诉我该对象的析构函数正在被调用 ,但是每次下一次分配都会增加该对象的地址,因此内存实际上并没有被释放... 请告诉我,为什么?!

I have to use Mac OS X 10.6.0 , XCode 3.2.1 , Apple GCC 4.2.1 . 我必须使用Mac OS X 10.6.0XCode 3.2.1Apple GCC 4.2.1

Is there a chance, that one of these custom frameworks redefines new and/or delete ? 这些自定义框架之一有可能重新定义new和/或delete吗? Try to add this replacements to your test program and look if a) the test program builds without complaining about multiple definitions of new and/or delete and b) it runs and shows a delete for every new? 尝试将这些替换项添加到您的测试程序中,看看是否a)测试程序在构建时不会抱怨new和/或delete的多个定义; b)它运行并显示每个新的删除?

 // not a real replacement, for testing purposes only
void * operator new (size_t sz) 
{
 printf("my new\n");
 return malloc(sz);
}
void operator  delete (void*ptr) 
{
 printf("my delete\n");
 free(ptr);
}
void operator  delete[] (void*ptr) 
{
 printf("my delete[]\n");
 free(ptr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM