c++ - Deleting allocated memory pointed by vector element crashes the program -


i´m writing dynamic program read oracle database metadata , build structure in memory.

i´m facing problem when freeing memory created new hold data oracle sends me. structure put inside vector , runs fine (i can read , process it).

here code (database stuff removed focusing on problem):

    struct oraclecolumnstruct {         std::string name;         ub2 ocitype;         fieldtype fieldtype;         int size;         char *buffer;         sb2  *indicator;         ocidefine *definehandler;     };      std::vector<oraclecolumnstruct> columns;  void allocatecolumns()  {         columncount = ... // whatever database          (unsigned int = 0; < columncount; i++)         {              ocitype = ... // whatever database             size = ... // whatever database              oraclecolumnstruct data;             data.name = "whatever";             data.ocitype = ocitype;             data.size = size;             data.buffer = new char[size];             data.indicator = new sb2;              columns.push_back(data);         } }  void freecolumns() {         (auto &column : columns)         {             if (column.buffer)                 delete column.buffer; <<<- crash here on 2nd interaction              if (column.indicator)                 delete column.indicator;         } } 

the crash message:

heap corruption detected: after normal block (#16497) @ 0x00261940. crt detected application wrote memory after end of heap buffer. 

when need free structure, frees first element , on second processing vector seens lost.

does deleting pointer reference confuses whole vector ? shall done in case ?

buffer allocated new[], must freed using delete[] instead of delete.

indicator allocated new, must freed using delete , not delete[].

and there no need check null, delete , delete[] handle you:

void freecolumns() {     (auto &column : columns)     {         delete[] column.buffer;         delete column.indicator;     } } 

that being said, should use std::vector , std::unique_ptr, can rid of freecolumns() altogether , call columns.clear() when needed.

struct oraclecolumnstruct {     std::string name;     ub2 ocitype;     fieldtype fieldtype;     std::vector<char> buffer;     std::unique_ptr<sb2> indicator;     ocidefine *definehandler; };  void allocatecolumns  {     columncount = ... // whatever database      (unsigned int = 0; < columncount; i++)     {         ...         oraclecolumnstruct data;         data.name = "whatever";         data.ocitype = ocitype;         data.buffer.resize(size);         data.indicator.reset(new sb2);         // or: data.indicator = std::make_unique<sb2>();          columns.push_back(data);        ...     } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -