2016-08-23 67 views
0

我得到訪問衝突讀取位置0xCDCDCDD1錯誤。我正在使用vc9編譯器。new_srdProg_d.exe中的0x0040224d未處理的異常:0xC0000005:訪問衝突讀取位置0xcdcdcdd1

我的代碼:

#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(行爲); 問題應該是在該行

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提前!

+3

爲什麼malloc的內C++? – user1810087

+2

你爲什麼要嘗試實例化ISRDPresenter,它是一個抽象類! –

+1

不要發送垃圾郵件標籤。 C不是C++不是C! – Olaf

回答

3

通過使用malloc而不是new,您跳過了對象的初始化。模式cdcdcdcd以調試模式編譯寫入新分配的內存,以幫助您檢測此類問題。你正試圖訪問一個虛擬函數,它正在訪問一個尚未初始化的vtable。

應該能夠做到:

ISRDPresenter* dev = new ASRDPresenter; 
+0

謝謝它是一個標頭問題,它現在適用於: ISRDPresenter * dev = new ASRDPresenter; –

相關問題