Oh perl monks,

I'm using the 'libunistring' library in C to uppercase utf8-encoded characters.

In C it works fine with no memory leaks according to Valgrind. But, when I try to do it in XS it works, but valgrind complains loudly.

Here's the C:

#include <stdio.h> #include <unicase.h> #include <stdlib.h> #include <unistr.h> #include <uninorm.h> #include <string.h> #include <unistr.h> #include <unitypes.h> void main(){ char* instring = "Hauptstraße 3"; // Works with uint8_t too size_t length; uint8_t *result; size_t input_length; input_length=strlen(instring); //u8_strlen works too result = u8_toupper (instring, input_length, NULL, UNINORM_NFC, NULL, +&length);

The result is as expected: HAUPTSTRASSE 3

my XS function (same includes as above):

SV* uppercase_utf8_2(SV* sv) PREINIT: size_t len; char* s; char* upperstring; //uint8_t also SV* aresult; size_t upperlength; CODE: s = SvPVbyte(sv, len); //strings from my editor (locale?) are alr +eady utf8 encoded upperstring = u8_toupper (s, len, NULL, NULL,NULL, &upperlength); upperlength = u8_strlen(upperstring); aresult = newSVpv(upperstring,upperlength); free(upperstring); RETVAL = aresult; OUTPUT: RETVAL

All hunky-dory except for a tonne of errors from Valgirind, none of which mention the XS sub

e.g:

==614897== Invalid read of size 1 ==614897== at 0x484F234: strlen (in /usr/libexec/valgrind/vgpreload +_memcheck-amd64-linux.so) ==614897== by 0x485B74A: XS_Simple__Ngram_uppercase_utf8_2 (Ngram.x +s:79) ==614897== by 0x2441A9: ??? (in /usr/bin/perl) ==614897== by 0x2396DD: Perl_runops_standard (in /usr/bin/perl) ==614897== by 0x1781DA: perl_run (in /usr/bin/perl) ==614897== by 0x14D639: main (in /usr/bin/perl) ==614897== Address 0x5bb9282 is 0 bytes after a block of size 18 allo +c'd ==614897== at 0x484DB80: realloc (in /usr/libexec/valgrind/vgpreloa +d_memcheck-amd64-linux.so) ==614897== by 0x55AEF01: libunistring_u8_casemap (in /usr/lib/x86_6 +4-linux-gnu/libunistring.so.5.0.0) ==614897== by 0x55AF478: u8_toupper (in /usr/lib/x86_64-linux-gnu/l +ibunistring.so.5.0.0) ==614897== by 0x485B73F: XS_Simple__Ngram_uppercase_utf8_2 (Ngram.x +s:78) ==614897== by 0x2441A9: ??? (in /usr/bin/perl) ==614897== by 0x2396DD: Perl_runops_standard (in /usr/bin/perl) ==614897== by 0x1781DA: perl_run (in /usr/bin/perl) ==614897== by 0x14D639: main (in /usr/bin/perl) ==614897== ==614897== ==614897== HEAP SUMMARY: ==614897== in use at exit: 2,544,437 bytes in 9,481 blocks ==614897== total heap usage: 33,789 allocs, 24,308 frees, 6,454,177 +bytes allocated ==614897== ==614897== 2 bytes in 1 blocks are possibly lost in loss record 1 of 1 +,342 ==614897== at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload +_memcheck-amd64-linux.so) ==614897== by 0x24EADB: Perl_sv_magicext (in /usr/bin/perl) ==614897== by 0x24ED0A: Perl_sv_magic (in /usr/bin/perl) ==614897== by 0x183F8C: Perl_gv_fetchpvn_flags (in /usr/bin/perl) ==614897== by 0x174ED2: perl_parse (in /usr/bin/perl) ==614897== by 0x14D55B: main (in /usr/bin/perl)

Any ideas?


In reply to Memory Leak with XS but not pure C by FrankFooty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.