2013-04-28 60 views
0

我有,我需要一个指针分配给指针数组代码,但是当我想安全地删除它,调试器给我的错误。删除指针数组:_BLOCK_TYPE_IS_VALID

enter image description here

我知道我不应该使用指针,但向量类的指针数组,但我不得不这样做这种方式。 我有两个类:A E B.在波黑我有这样的:

private: A **aObject; 

在B.cpp我有这样的:

aObject = new A*[size]; 
for(int i=0; i<size; i++) { 
    aObject[i] = new A(); 
} 

当我想删除它,我这样做:

for(int i=0; i<size; i++) { 
    delete [i]aObject; // <----error debuger 
} 
delete []aObject; 

我已经阅读了很多讨论这个问题的主题,看起来我使用的方法是正确的。 Debuger在第62行delete [i]aObject;处返回错误。
那么,你有任何想法解决它吗? 此外,i为1时返回错误.A对象的私人成员(aObject)为CXX0030
看起来我正在寻址我的地址空间以外的内存区域,aObject是一个空指针,因此调试将返回堆中的错误。
谢谢你们。

编辑:
Okei,用delete aObject[i]我不再有这样的错误,但我有另一个错误:0xc0000005: Access violation reading location 0x0094e368。现在返回错误时i为0的错误总是在行aObject 62私有成员CXX0017aObject0x0094e368

编辑2:
我还没有定义任何A析构函数,只有默认的一个,但我不记得任何地方它。

A::~A(void){} 

编辑3: B.h,又名bci.h(对不起我的错)

#pragma once 

#include "thinkgear.h" 
#include <time.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include "eeg.h" 
#include <string> 

class bci { 

public: 

    bci(float timeAcquisition); 
    ~bci(void); 

    // FUNZIONI BCI // 
    // -> Connessione e setting della bci <- // 
    void autoInit(void); 
    int getDriverVersion(void); 
    void setNumPortCom(void); 
    void setNumPortCom(int portNumber); 
    void getConnectionId(void); 
    void connect(void); //Stablisce una connessione alla bci e setta connectionId 
    void setStreamLog(const char *filename); 
    void setStreamLog(void); 
    void setDataLog(const char *filename); 
    void setDataLog(void); 
    void enableLowPassFilter(void); 
    void disableLowPassFilter(void); 
    void enableBlinkDetection(void); //Abilita il blink detection 
    void disableBlinkDetection(void); //Disabilit il blink detection 
    void disconnected(void); //Libera la connessione con la bci 

    // -> Lettura e storing EEG <- // 
    void readAllEEGValues(void); 
    void storeAllEEGValues(void); 
    void readStoreAllEEGValues(void); 

    // -> Lettura e storing EEG (escluso segnale RAW) <- // poor signal e blink detectection inclusi 
    void readEEGValues(void); //Legge i dati dalla bci e aggiorna la variabili 
    void storeEEGValues(void); 
    void readStoreEEGValues(void); 

    // -> Lettura e storing segnale RAW <- // 
    void readEEGRawValue(void); 
    void storeEEGRawValues(void); 
    void readStoreEEGRawValues(void); 

    // -> Lettura e storing eSense Meter (attention e meditation) <- // poor signal e blink detectection inclusi 
    void readESenseValues(void); 
    void storeESenseValues(void); 
    void readStoreESenseValues(void); 

    // METODI GETTER E SETTER // 
    // -> Getter e Setter per i dati estratti <- // 
    time_t getTimestamp(int i); 
    float getLevelAttention(int i); //Restituisce il livello attuale di attenzione 
    float getLevelMeditation(int i); //Restituisce il livello attuale di meditazione 
    float getLevelRaw(int i); 
    float getLevelDelta(int i); 
    float getLevelTheta(int i); 
    float getLevelAlpha1(int i); 
    float getLevelAlpha2(int i); 
    float getLevelBeta1(int i); 
    float getLevelBeta2(int i); 
    float getLevelGamma1(int i); 
    float getLevelGamma2(int i); 
    float getBlinkStrengthDetection(int i); //Restitisce il livello attuale del blink detection (o dell'ultimo blink) 
    time_t* getTimestamp(void); 
    float* getLevelAttention(void); //Restituisce il livello attuale di attenzione 
    float* getLevelMeditation(void); //Restituisce il livello attuale di meditazione 
    float* getLevelRaw(void); 
    float* getLevelDelta(void); 
    float* getLevelTheta(void); 
    float* getLevelAlpha1(void); 
    float* getLevelAlpha2(void); 
    float* getLevelBeta1(void); 
    float* getLevelBeta2(void); 
    float* getLevelGamma1(void); 
    float* getLevelGamma2(void); 
    float* getBlinkStrengthDetection(void); //Restitisce il livello attuale del blink detection (o dell'ultimo blink) 
    bool checkBlinkDetection(void); //Controlla se è avvenuto o meno un bink 
    // -> Getter e Setter per gli errori <- // 
    int getIdConnection(void); 
    int getErrorStreamLog(void); 
    int getErrorDataLog(void); 
    int getErrorLowPassFilter(void); 
    int getErrorBlinkDetection(void); 
    int getErrorConnect(void); 
    int getPacketsRead(void); 
    int getPacketsAnalize(void); 

    // LETTURA ERRORI // 
    // -> Interpretato gli errori ricevuti <- // 
    std::string getStringErrorConnectionId(void); 
    std::string getStringErrorStreamLog(void); 
    std::string getStringErrorDataLog(void); 
    std::string getStringErrorLowPassFilter(void); 
    std::string getStringErrorBlinkDetection(void); 
    std::string getStringErrorConnect(void); 
    /*std::string getStringPacketsRead(void); 
    std::string getStringPacketsAnalize(void);*/ 

    // -> Printing di valori estratti e di variabili utili <- // 
    void printBciInfo(void); //Stampa alcune informazioni utili della classe 
    void printValues(void); 
    void printDataValues(void); 
    void printESenseValues(void); 

    // -> Metodo setter e getter per timeAcquisition <- // 
    // * Numero di campionamenti da fare, o numero di secondi di running della bci * // 
    void setTimeAcquisition(int time); 
    int getTimeAcquisition(void); 

    // -> Salva il segnale EEG <- // 
    // * num viene utilizzato nel seguente modo: se num=0, stampa tutti tranne raw; num=1, stampa solo il raw; num=2, stampa tutto. 
    void saveToCsv(const char *filename, int num); 
    void saveToCsv(int num); 

    // -> Altro <- // 
    float* convertRawToVoltage(void); //Converte la matrice data che contiene tutti i segnali raw ai rispetti valori in Volt. 
    float convertRawToVoltage(float rawValue); //Converte il valore raw e ritorna il rispettivo valore in Volt. 

    // -> Prova <- // 
    //eeg** 

    // -> Non implementata <- // 
    //void cleanDataFile(void); 

    /*void cleanCSVFile(const char *filename); 
    std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems); 
    std::vector<std::string> split(const std::string &s, char delim);*/ 

private: 

    int connectionId; //Id della connessione alla bci. 0 ok, -2 no free memory to allocate connection, -1 to many connection without TG_FreeConnection() 
    int dllVersion; 
    int numPortCom; 

    time_t *timer; 
    float *attention; //Livello di attenzione attuale 
    float *blinkStrength; //Livello di blink attuale 
    bool blink; //E' a 'true' se si è verificato un blink, 'false' altrimenti, 
    float *meditation; //Livello di meditazione attuale 
    float *poorSignal; //Qualità del segnale ricevuto 
    float *raw; 
    float *delta; 
    float *theta; 
    float *alpha1; 
    float *alpha2; 
    float *beta1; 
    float *beta2; 
    float *gamma1; 
    float *gamma2; 

    int errorStreamLog; //-1 invalid id connection, -2 error with file, 0 success 
    int errorDataLog; //-1 invalid id connection, -2 error with file, 0 success 
    int errorLowPassFilter; //-1 invalid id connection, -2 rawSamplingRate is not a valid rate, 0 success 
    int errorBlinkDetection; //-1 invalid id connection, 0 success 
    int errorConnect; //-1 invalid id connection, -2 serialPortName could not be opened, -3 if serialBaudrate is not a valid TG_BAUD_* value, -4 if @c serialDataFormat is not a valid TG_STREAM_* type, 0 success 
    int errorReadPackets; //-1 invalid id connection, -2 if there were not even any bytes available to be read, -3 if an I/O error occurs 

    int packetsRead; //Handler per la lettura dei dati dalla bci 
    int packetsAnalize; //Contatore per il numero totale di pacchetti letti dalla bci 

    int timerAcquisition; 

    eeg **data; 

}; 

编辑4:
也许我发现这个问题。 在主函数中我这样做:

int main(void) { 
     ... 
    B *bObject=new B(timeAcquisition); 
     ... 
    bObject->~B(); 
    delete bObject; //<--- THIS! 
} 

这是可能的吗?没有delete bObject;调试器不会返回任何错误。

编辑5: 所以,答案是here

Using the delete operator also causes the class destructor (if there is one) to be called.

+5

这是什么'删除[I] aObject;'? – 2013-04-28 17:49:25

+2

'删除aObject [i];'会更有意义。 – 2013-04-28 17:50:21

+6

使用RAII会更有意义。 – chris 2013-04-28 17:54:01

回答

1

所以,解决的办法是here

Using the delete operator also causes the class destructor (if there is one) to be called.

int main(void) { 
     ... 
    B *bObject=new B(timeAcquisition); 
     ... 
    delete bObject; 
}