Access violation when freeing char* in tesseract, c++ -


using tesseract ocr, part of code:

        pix *pix = pixread("mylocation/my.jpg");          api.setimage(pix);         char* result = new char[100];         result = api.getutf8text();         new1 = atof(result);          cout << "result: " << new1 << endl;          delete[] result; 

i access violation when delete result.

i saw post suggesting "rebuilt tesseract in vs2015", when using vs2015. how that?

any appreciated. thanks.

simply put, not have ownership of result delete object. i'll explain why.

on line 4, char* result = new char[100]; allocate 100 bytes of memory, , store location in result pointer. assume memory address happens 0xf00.

on line 5, result = api.getutf8text(); method (appears to) return pointer, means memory-address in 'result' being over-written new pointer. perhaps location of internal-buffer provided api 0xba2. haven't stored anywhere else, 0xf00 address lost!

on line ten, delete[] result; - tells system delete memory pointed-to result, in case, hypothetical 0xba2, memory owned api, not memory allocated - system detected this, , threw appropriate error.

if api returns pointer

in case, can replace lines 4 , 5 char * result = api.getutf8text(). , remove delete statement entirely.

some helpful advice:

note modern c++ (and compilers support) shared_ptr, unique_ptr, , others - it's better use these, rather free/delete.

if leave resource management handles , containers relying on raii, rather littering code pointer, news , deletes, don’t encounter resource leaks or write freed memory. ~ bjarne stroustrup


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -