我收到Access违规读取位置0xCDCDCDD1错误。我正在使用vc9编译器。
我的代码:
#define SRD_EXPORTS
#include "SRD.h"
#include "ISRDPresenter.h"
#include "ISRDFactory.h"
#include "ISRDevice.h"
#include "AString.h"
int main( int argc, char** argv )
{
ISRDPresenter* dev = (ISRDPresenter*)malloc(sizeof(ISRDPresenter)); // = NewSRDPresenter();
char act[ 128 ];
int commands = argc-1;
do
{
if( !commands )
{
act[ 0 ] = 0;
printf("SRD> ");
#if defined(__IBMC__) || defined (__IBMCPP__)
fflush(stdout);
#endif
fgets( act, 128, stdin );
}
else
{
strncpy( act, argv[ argc - commands-- ], 128 );
printf("SRD> %s\n", act);
}
if( !strncmp( act, "help", 4 ) )
{
dev->showHelp( act );
}
...
}
错误在行dev-> showHelp(act); 问题应该在
行ISRDPresenter* dev = (ISRDPresenter*)malloc(sizeof(ISRDPresenter));
但我无法修复它。 如果我改用:
ISRDPresenter* dev = NewSRDPresenter();
它说标识符无法找到,如果我使用:
ISRDPresenter* dev = new ISRDPresenter();
说ISRDPresenter无法实例化抽象类......
ISRDPresenter.h:
#ifndef __ISRDPRESENTER_H
#define __ISRDPRESENTER_H
#define SRD_EXPORTS
#include "SRD.h"
#include "ISRDDataTypes.h"
#include "AIPClass.h"
class ISRDFactory;
class ISRDPresenter
{
public:
// -----------------------------------------------------------------------
// Destructor
// -----------------------------------------------------------------------
virtual ~ISRDPresenter() {};
// -----------------------------------------------------------------------
// Function prototypes
// -----------------------------------------------------------------------
virtual void showHelp( char* attr ) const = 0;
virtual void showList( void ) const = 0;
virtual void showInfo( size_t i ) const = 0;
virtual void showFirmware( size_t i ) const = 0;
};
#endif /* __ISRDPRESENTER_H */
SRDPresenter.h:
#ifndef __SRDPRESENTER_H
#define __SRDPRESENTER_H
#define SRD_EXPORTS
#include "SRD.h"
#include "ISRDPresenter.h"
#include "AString.h"
#include "ISRDFactory.h"
//-----------------------------------------------------------------------------
// ASRDPresenter
//-----------------------------------------------------------------------------
class ASRDPresenter : public ISRDPresenter
{
public:
//-------------------------------------------------------------------------
// ASRDPresenter constructor & destructor
//-------------------------------------------------------------------------
ASRDPresenter();
virtual ~ASRDPresenter(){};
//-------------------------------------------------------------------------
// Function prototypes
//-------------------------------------------------------------------------
void showHelp( char* attr ) const;
void showList( void ) const;
void showInfo( size_t i ) const;
void showFirmware( size_t i ) const;
private:
ISRDFactory* f_srds;
};
SRD_EXPORT_PREF ISRDPresenter* SRD_EXPORT_POST NewSRDPresenter()
{
return new ASRDPresenter();
}
#endif /* __SRDPRESENTER_H */
SRDPresenter.cpp:
#define SRD_EXPORTS
#include "SRDPresenter.h"
#if defined(_MSC_VER)
#define WIN32_LEAN_AND_MEAN
#endif
// ---------------------------------------------------------------------------
// ASRDPresenter
// ---------------------------------------------------------------------------
ASRDPresenter::ASRDPresenter()
:f_srds( )
{
// empty
}
void ASRDPresenter::showHelp( char* attr ) const
{
if( !strcmp( attr, "help" ) )
{
printf( "\nHelp to the ANOVIS SRD command.\n\n" );
printf( " help : Print this help.\n" );
printf( " list : List all ANOVIS SRD found.\n" );
printf( " info
}
...
}
我该如何解决?光! Thanx提前!
答案 0 :(得分:3)
使用malloc
代替new
,您已跳过对象的初始化。模式cdcdcdcd
在调试模式构建中写入新分配的内存,以帮助您检测此类问题。您正在尝试访问虚拟功能,该功能正在访问尚未初始化的vtable。
你 应该能够:
ISRDPresenter* dev = new ASRDPresenter;