C++ 操作员新建导致初始化列表中的崩溃
我在C++中遇到了一个奇怪的问题: 当我想在我的C++ 操作员新建导致初始化列表中的崩溃,c++,sdl,C++,Sdl,我在C++中遇到了一个奇怪的问题: 当我想在我的PixelIterator类的初始值设定项列表中新建uint8\t*时,我的程序崩溃 以下是调用堆栈: #0 00000000 0x0040b030 in __cxa_throw() (??:??) #1 00000000 0x004047e1 in operator new() (??:??) #2 004018B5 PixelIterator(this=0x28fe2c, _oth=0x5d5154, iOffset=300, iPitch=3
PixelIterator
类的初始值设定项列表中新建uint8\t*
时,我的程序崩溃
以下是调用堆栈:
#0 00000000 0x0040b030 in __cxa_throw() (??:??)
#1 00000000 0x004047e1 in operator new() (??:??)
#2 004018B5 PixelIterator(this=0x28fe2c, _oth=0x5d5154, iOffset=300, iPitch=300, iBpp=2 '\002') (C:\Users\David\Desktop\SDL++\SDLPP_Sprite.cpp:34)
#3 0041E0CC SDL::Sprite::At(this=0x5d1548, iX=150, iY=0) (C:/Users/David/Desktop/SDL++/SDLPP_Sprite.hpp:89)
#4 0041E5B0 MyApp::OnInit(this=0x5d1520, iArgc=1, ppcArgv=0x5d1498) (C:\Users\David\Desktop\SDL++\main.cpp:37)
#5 00402641 SDL::Application::Run(this=0x5d1520, pScreen=0x5d4df0, iArgc=1, ppcArgv=0x5d1498) (C:\Users\David\Desktop\SDL++\SDLPP_Application.cpp:24)
#6 004020A3 main(iArgc=1, ppcArgv=0x5d1498) (C:\Users\David\Desktop\SDL++\main.cpp:103)
首先,PixelIterator
类继承自BaseIterator
:
template< typename T >
class BaseIterator
{
public:
typedef T ValueType;
BaseIterator( void )
: ptr( NULL )
{}
BaseIterator( const BaseIterator< T >& _oth )
: ptr( _oth.ptr )
{}
BaseIterator( T * p )
: ptr( p )
{}
T& operator*( void )
{
assert( ptr != NULL );
return *ptr;
}
T* operator->( void )
{
assert( ptr != NULL );
return ptr;
}
bool operator==( const BaseIterator< T >& _oth )
{
return ptr == _oth.ptr;
}
bool operator!=( const BaseIterator< T >& _oth )
{
return ptr != _oth.ptr;
}
bool operator<( const BaseIterator< T >& _oth )
{
return ptr < _oth.ptr;
}
bool operator<=( const BaseIterator< T >& _oth )
{
return ptr <= _oth.ptr;
}
bool operator>( const BaseIterator< T >& _oth )
{
return ptr > _oth.ptr;
}
bool operator>=( const BaseIterator< T >& _oth )
{
return ptr >= _oth.ptr;
}
friend ostream& operator<<( ostream& _os, const BaseIterator< T >& _this )
{
_os << reinterpret_cast< void* >( _this.ptr );
return _os;
}
protected:
T * ptr;
};
这是构造函数,错误发生在这里:
Sprite::PixelIterator::PixelIterator( uint8_t ** _oth, uint32_t iOffset, uint16_t iPitch, uint8_t iBpp )
: BaseIterator< uint8_t* >( new uint8_t*( *_oth + iOffset ) ),
m_iPitch( iPitch ),
m_iBpp( iBpp )
{}
SDL::Sprite::Iterator
与SDL::Sprite::PixelIterator
(typedef
)相同。
m_Img
是SDL::Sprite
的对象
SDL::Sprite::At()
方法:
inline
Iterator At( uint32_t iX, uint32_t iY )
{
return Iterator( reinterpret_cast< uint8_t ** >( &m_pSurface->pixels ),
iY * m_pSurface->pitch + iX * m_pSurface->format->BytesPerPixel,
m_pSurface->pitch, m_pSurface->format->BytesPerPixel );
}
内联
迭代器At(uint32_t iX,uint32_t iY)
{
返回迭代器(重新解释强制转换(&m\u pSurface->像素),
iY*m_pSurface->音高+iX*m_pSurface->格式->字节/像素,
m_pSurface->音高,m_pSurface->格式->字节/像素);
}
<代码> MypSurvie<代码>是一个指针,在<代码> SDLIOFSUTS/<代码> < /P>如果新崩溃,它可能是程序另一部分中重写内存的问题。如果你有C++问题,你为什么用
c
标记这个问题?你可能想向我们展示一下你是如何实例化PixelIterator
的。为什么BaseIterator
持有一个uint8**
,为什么new
只持有一个指针,而不是按值持有它?(顺便说一句,你也在泄露指针)我更新了帖子@它包含一个uint8_t**,因为我需要一个uint8_t*上的指针,我需要将这个指针(uint8_t*)转换为不同的指针类型:uint16_t*用于16 BPP图像,uint32_t*用于32 BPP图像。我为什么要泄密?在复制之前,我在析构函数中释放内存。
m_Img.Lock();
for( int32_t y = 0; y < m_Img.GetSurface()->h; y++ )
{
for( SDL::Sprite::Iterator x = m_Img.At( 0, y ); x != m_Img.At( m_Img.GetSurface()->w, y ); x++ )
*reinterpret_cast< uint16_t* >( *x ) = 0;
}
m_Img.Unlock();
inline
Iterator At( uint32_t iX, uint32_t iY )
{
return Iterator( reinterpret_cast< uint8_t ** >( &m_pSurface->pixels ),
iY * m_pSurface->pitch + iX * m_pSurface->format->BytesPerPixel,
m_pSurface->pitch, m_pSurface->format->BytesPerPixel );
}