Bee Chi K
Bee Chi K

Reputation: 3

Unhandled exception at 0x0040224d in new_srdProg_d.exe: 0xC0000005: Access violation reading location 0xcdcdcdd1

I am getting the Access violation reading location 0xCDCDCDD1 error. I´m using a vc9 compiler.

My code:

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

The error is in the line dev->showHelp(act); The problem should be in the line

ISRDPresenter* dev = (ISRDPresenter*)malloc(sizeof(ISRDPresenter));

But I can´t fix it. If I use instead:

ISRDPresenter* dev = NewSRDPresenter();

It says identifier cannot be found, and if I use:

ISRDPresenter* dev = new ISRDPresenter();

says that ISRDPresenter cannot instantiate abstract class...

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        
    }    
...
}

How can I fix it? Light! Thanx in advance!

Upvotes: 0

Views: 937

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308452

By using malloc instead of new, you've skipped the initialization of the object. The pattern cdcdcdcd is written to freshly allocated memory in debug mode builds to help you detect this kind of problem. You're trying to access a virtual function, which is accessing a vtable which hasn't been initialized.

You should be able to do:

ISRDPresenter* dev = new ASRDPresenter;

Upvotes: 3

Related Questions