aslze / asl

A compact C++ cross-platform library including JSON, XML, HTTP, Sockets, WebSockets, threads, processes, logs, file system, CSV, INI files, vectors and matrices, etc.
Other
69 stars 17 forks source link

chars() problem #21

Closed dezashibi closed 1 year ago

dezashibi commented 1 year ago

when String contains any utf8 characters like an emoji or a character from another language chars() works as expected, but when String contains only ascii characters, calling chars() causes the following error, and it seems it has something related to memory clean up or in the destructor.

the error: HEAP CORRUPTION DETECTED: after Normal block (....) at .........
CRT detected that the application wrote to memory after end of heap buffer.
    asl::String a("a__01");

    auto c = a.chars();
    std::cout << c.length() << std::endl;
    std::cout << a.count() << std::endl;
//  for (int i{0}; i < a.count(); ++i)
//      std::cout << c[i] << std::endl;
        std::getchar();

this code for example, this works until pressing a key, and what happens after is just destructing c and a. I'm checking this line int n = utf8toUtf32(str(), c.ptr(), 1); that I think it might be this.

aslze commented 1 year ago

I think it is now fixed. That function writes a null character at the end and there was no space for it.

dezashibi commented 1 year ago

yes it's fixed now, thanks sā­

aslze commented 1 year ago

BTW, in a loop like that it is better to use

for (int i{0}; i < c.length(); ++i)

instead of:


for (int i{0}; i < a.count(); ++i)

because count() has to go through the string every time, but c.length() (the array of code points) is already computed.

dezashibi commented 1 year ago

Definitely, it wasn't the actual code, I was testing to make sure the count() also is correct. Thanks anyway šŸ™

On Tue, Jul 18, 2023, 10:26 aslze @.***> wrote:

BTW, in a loop like that it is better to use

for (int i{0}; i < c.length(); ++i)

instead of:

for (int i{0}; i < a.count(); ++i)

because count() has to go through the string every time, but c.length() (the array of code points) is fixed.

ā€” Reply to this email directly, view it on GitHub https://github.com/aslze/asl/issues/21#issuecomment-1639580062, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANVCKEE6GZEIDX5I4ETTR3DXQYUCJANCNFSM6AAAAAA2N2MYVA . You are receiving this because you modified the open/close state.Message ID: @.***>