0

我在 C++ 中有一个奇怪的问题:

当我想uint8_t*在我的类的初始化列表中新建 a 时,我的程序崩溃了PixelIterator

这是调用堆栈:

#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;
};

这是PixelIterator课程:

/**
 **  Designed to iterate through a virtual 2D array of pixels( uint8_t* )
 **/
class PixelIterator : public BaseIterator< uint8_t* >
{
public:
    PixelIterator( void );
    PixelIterator( const PixelIterator& _oth );
    PixelIterator( uint8_t ** _oth, uint32_t iOffset, uint16_t iPitch, uint8_t iBpp );
    ~PixelIterator( void );

    PixelIterator& operator=( const PixelIterator& _oth );

    PixelIterator& operator++( void ); // 1 pixel right
    PixelIterator& operator--( void ); // 1 pixel left
    PixelIterator& operator++( int unused ); // 1 pixel right
    PixelIterator& operator--( int unused ); // 1 pixel left

    PixelIterator& operator+=( uint32_t iSize ); // iSize pixel right
    PixelIterator& operator-=( uint32_t iSize ); // iSize pixel left

    PixelIterator& operator>>( uint32_t iSize ); // iSize pixel down
    PixelIterator& operator<<( uint32_t iSize ); // iSize pixel up

protected:
    uint16_t m_iPitch;
    uint8_t m_iBpp;
};

这是构造函数,错误发生的地方:

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 )
{}

_oth在这种情况下是指向像素数据的指针(一维数组void*); iOffset是数组中的偏移量

我使用 SDL 进行图形输出,使用 Code::Blocks 作为 IDE。测试图像的大小为 150x120 像素。

如果您需要有关代码的更多信息,请询问:)

编辑:

实例化:

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();

SDL::Sprite::IteratorSDL::Sprite::PixelIterator与( )相同typedefm_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 );
}

m_pSurface是一个指针SDL_Surface

4

0 回答 0