超载并删除会导致valgrind错误

问题描述 投票:0回答:1

Valgrind在抱怨这段代码,我真的不明白为什么吗?

==9144==ERROR: AddressSanitizer: attempting double-free on 0x60200000eff0 in thread T0:

完整logs

我正在为代码使用自定义分配器,但下面是它的较短版本,它显示相同的症状。如果我在嵌入式系统上运行,则不需要释放内存,但是我始终想确保在基于x86的系统上运行时,我不会泄漏任何内存,并且我正在使用valgrind进行验证。

/*
g++ test.cpp -fsanitize=address -fno-omit-frame-pointer
./a.out
*/

 #include <iostream>                                                                                                                                                                                        

 class TestMemoryPool{                                                                                                                                                                                      
 };                                                                                                                                                                                                         

 void* operator new(size_t n, TestMemoryPool* pool)                                                                                                                                                         
 {                
 #ifdef CUSTOM_ALLOCATER
     //snip
 #else                                                                                                                                                                                                                                                                                                                                                             
     void *tmp = ::operator new(n);                                                                                                                                                                         
     return tmp;  
 #endif                                                                                                                                                                                          
 }                                                                                                                                                                                                          

 void* operator new[](size_t n , TestMemoryPool* pool)                                                                                                                                                      
 {         
 #ifdef CUSTOM_ALLOCATER
     //snip
 #else                                                                                                                                                                                                                                                                                                                                                                      
     void *tmp = ::operator new(n);                                                                                                                                                                         
     return tmp;   
 #endif                                                                                                                                                                                         
 }                                                                                                                                                                                                          

 void operator delete(void* ptr, TestMemoryPool* pool)                                                                                                                                                      
 {         
 #ifdef CUSTOM_ALLOCATER
     //snip
 #else                                                                                                                                                                                                                                                                                                                                                                      
     ::operator delete(ptr);       
 #endif                                                                                                                                                                         
 }                                                                                                                                                                                                          

 void operator delete[](void* ptr , TestMemoryPool* pool)                                                                                                                                                   
 {          
 #ifdef CUSTOM_ALLOCATER
     //snip
 #else                                                                                                                                                                                                                                                                                                                                                                  
     ::operator delete(ptr);   
 #endif                                                                                                                                                                             
 }                                                                                                                                                                                                          

 int main()                                                                                                                                                                                                 
 {                                                                                                                                                                                                          
     TestMemoryPool *pool;                                                                                                                                                                                  
     float *signalGen = new (pool) float[100];                                                                                                                                                              
     TestMemoryPool *p = new (pool) TestMemoryPool();                                                                                                                                                       
     operator delete[](signalGen, pool);                                                                                                                                                                    
     operator delete(p, pool);                                                                                                                                                                              
     delete p;                                                                                                                                                                                              
 }     
c++ valgrind
1个回答
0
投票

由于两次删除指针p,所以出现了双重释放错误:

operator delete(p, pool);
delete p;
© www.soinside.com 2019 - 2024. All rights reserved.