Thiyagu has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am using below code to read the value of array of strings to the perl script.

Everything works fine, if the perl array size is less than 127. If the array size crosses 127. Abort is seen in sv_setpv function (Heap Corruption in Windows).

SWIG Perl Code to read the character array from the c++ library to perl:

{ int i = 0; while (result[i]) { if(i+1>items){ EXTEND(sp,1); } ST(argvi+i) = sv_newmortal(); sv_setpv((SV *)ST(argvi+i),result[i]);  Abort is hap +pening this point delete [] result[i]; i++; } argvi+=i; delete [] result; }

Is there any limitation in stack size of something else which is causing the issue ?.

Is there is a way overcome this issue ?.

Crash Dump:

FAULTING_THREAD: 0000000000000c5c DEFAULT_BUCKET_ID: HEAP_CORRUPTION PRIMARY_PROBLEM_CLASS: HEAP_CORRUPTION BUGCHECK_STR: APPLICATION_FAULT_HEAP_CORRUPTION_NULL_CLASS_PTR_DEREFE +RENCE_INVALID_POINTER_READ_WRONG_SYMBOLS_FILL_PATTERN_ffffffff LAST_CONTROL_TRANSFER: from 00007ffb262d21b2 to 00007ffb262d23c3 STACK_TEXT: 00000000`001bee80 00007ffb`262d21b2 : 00000000`02000002 00007ffb`00000 +000 00000000`000000a8 00000000`00000000 : ntdll!RtlAllocateHeap+0x2f3 00000000`001bef30 00007ffb`25f01d36 : 00000000`00000001 00000000`00000 +002 00000000`000000a8 00000000`00000000 : ntdll!RtlAllocateHeap+0xe2 00000000`001bf040 00000000`5825996d : 00000000`00000090 00000000`007a5 +df8 00000000`00000001 00000000`585125f0 : msvcrt!malloc+0x56 00000000`001bf070 00000000`58262a9a : 00000000`00000002 00000000`5848d +ef1 00000000`00000000 00000000`000000c5 : perl516!VMem::Malloc+0x1d [ +e:\codavs05\hpsw-opcsi\external\perl\5.16.0\win5.2_64\perl-5.16.0\win +32\vmem.h @ 166] 00000000`001bf0a0 00000000`5829bee5 : 00000000`022c2630 00000000`00000 +090 00000000`007a5df8 00000000`01dd44f0 : perl516!Perl_safesysmalloc+ +0x4a [e:\codavs05\hpsw-opcsi\external\perl\5.16.0\win5.2_64\perl-5.16 +.0\util.c @ 102] 00000000`001bf0d0 00000000`5828e521 : 00000000`022c2630 00000000`007a5 +df8 00000000`00000003 00000000`00000000 : perl516!Perl_sv_grow+0x135 +[e:\codavs05\hpsw-opcsi\external\perl\5.16.0\win5.2_64\perl-5.16.0\sv +.c @ 1521] 00000000`001bf100 00007ffb`1d87cf03 : 00000000`007a5df8 00000000`00000 +003 00000000`022c5dc8 00000000`00000000 : perl516!Perl_sv_setpv+0xb1 +[e:\codavs05\hpsw-opcsi\external\perl\5.16.0\win5.2_64\perl-5.16.0\sv +.c @ 4550]

suggest me, is there is a way to read the array for more than 127 array size.

Thanks and Regards,

Thiyagu

2018-08-02 Athanasius added code and paragraph tags

Replies are listed 'Best First'.
Re: sv_setpv aborts when the SV (perl output array)have more than 127 size
by dave_the_m (Monsignor) on Jul 31, 2018 at 13:35 UTC
    You aren't extending the stack correctly. Each EXTEND(sp,1) is making sure there is one space on the stack beyond sp, but since sp isn't incremented each time, the EXTENDs don't accumulate.

    Dave.

Re: sv_setpv aborts when the SV (perl output array)have more than 127 size
by Corion (Patriarch) on Jul 31, 2018 at 12:54 UTC

    It's a bit hard to read your code as presented. Please use <code>...</code> around your code to make it render as a code block.

    I'm not sure that your code is actually sound, as you seem to have objects stored in the result array, but then copy pointers to Perl space (via sv_setpv) and also call delete [] on them, if I understand the C++ code correctly. But that leaves the memory in a very bad state, because delete [] will also have called the destructor on the object referenced by result[i], and you still have a pointer to that memory location stored in ST(argvi+i). This will result in memory corruption and many things can happen there.

    { int i = 0; while (result[i]) { if(i+1>items){ EXTEND(sp,1); } ST(argvi+i) = sv_newmortal(); sv_setpv((SV *)ST(argvi+i),result[i]); # Abort is happening +this point delete [] result[i]; i++; } argvi+=i; delete [] result; }

    My suggestion would be to remove all calls to delete until you get things working and then add the delete [] result; back, maybe. But even then you need to make sure that the contents of result themselves are not removed, because they are now referenced from Perl space.

Re: sv_setpv aborts when the SV (perl output array)have more than 127 size
by anonymized user 468275 (Curate) on Jul 31, 2018 at 13:34 UTC
    I haven't delved into this area of Perl, but heaps are meant for dynamic allocation of individual records, rather than arrays of records. So it seems as a pure guess, you should either allocate each record individually in a linked list or use the stack to make it function like an array.

    One world, one people