operator new会导致初始化列表崩溃

时间:2013-01-02 11:24:05

标签: c++ sdl

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

当我想在uint8_t*课程的初始化列表中新建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*的1D数组); 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::PixelIteratortypedef)相同。 m_ImgSDL::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_pSurfaceSDL_Surface

上的指针

0 个答案:

没有答案
相关问题