lezan
lezan

Reputation: 787

Delete of array of pointer: _BLOCK_TYPE_IS_VALID

I have a code in which I need to allocate a pointer to an array of pointers, but when I want to be safely deleted it, debugger gives me error.

enter image description here

I know I should not use a pointer array of pointers but the vector class, but I had to do it in this way. I have two classes: A e B. In B.h I have this:

private: A **aObject;

In B.cpp I make this:

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

When I want to deleted it, I do this:

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

I have read many threads that talking about that and it seems that the method I use is correct. Debuger return an error at line 62 delete [i]aObject;.
So, have you any idea for fix it? Moreover, it return error when i is 1. Private members of A object (aObject) are CXX0030.
Looks like I'm addressing an area of memory out of my address space, aObject is a null pointer, so the debugging returns the error on heap.
Thanks guys.

EDIT:
Okei, with delete aObject[i] I no longer have this error, but I have another error: 0xc0000005: Access violation reading location 0x0094e368. Now return error when i is 0. Error always at line 62. Private members of aObject are CXX0017 and the value of aObject is 0x0094e368.

EDIT 2:
I have not defined any destructor for A, only the default one, but I do not recall it anywhere.

A::~A(void){}

EDIT 3: B.h, aka bci.h (sorry for my mistake)

#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;

};

EDIT 4:
Maybe I found the problem. In the main function I do this:

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

It is possible? Without delete bObject; debugger does not return any error.

EDIT 5: So, the answer is here:

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

Upvotes: 0

Views: 2369

Answers (1)

lezan
lezan

Reputation: 787

So, the solution is 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;
}

Upvotes: 1

Related Questions