Hi! I am new commer of XS, I have write a .xs code but it looks have memoryleak. I don't know how to debug it and where is the cause of this problem. Can somebody help me. the top_insert.xs file is:

#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "const-c.inc" #include "top_insert.h" MODULE = top_insert PACKAGE = top_insert INCLUDE: const-xs.inc void top_insert(up_v, down_v, init_step_ratio, ratio) SV * up_v SV * down_v double init_step_ratio double ratio INIT: int up_num, down_num, sample_num; int i, n; double *up_data = NULL; double *down_data = NULL; double *return_val = NULL; int *return_num = &sample_num; if((!SvROK(up_v)) || (SvTYPE(SvRV(up_v)) != SVt_PVAV) || ((up_num = av_len((AV *)SvRV(up_v))) < 0)){ XSRETURN_UNDEF; } if((!SvROK(down_v)) || (SvTYPE(SvRV(down_v)) != SVt_PVAV) || ((down_num = av_len((AV *)SvRV(down_v))) < 0)){ XSRETURN_UNDEF; } PPCODE: up_data = safemalloc(up_num*sizeof(double)); down_data = safemalloc(down_num*sizeof(double)); for (n = 0; n <= up_num; n++) { *(up_data+n) = SvNV(*av_fetch((AV *)SvRV(up_v), n, 0)); } for (n = 0; n <= down_num; n++) { *(down_data+n) = SvNV(*av_fetch((AV *)SvRV(down_v), n, 0)) +; } up_num = up_num/3; down_num = down_num/3; return_val = get_inter_line(up_num, up_data, down_num, down_da +ta, init_step_ratio, ratio, return_num); safefree(up_data); safefree(down_data); sample_num = sample_num*3; for (n=0; n<sample_num; n++){ XPUSHs(sv_2mortal(newSVnv(*(return_val + n)))); } del_inter_line(return_val);

the top_insert.cpp is:

#include <vector> #include "top_insert.h" #include "geo.h" #include "standard_contour.h" double * get_inter_line(int up_num, double * up_data, int down_num, double *down_data, double init_step_ratio, double ratio, int * return_num ) { std::vector<point<double> > up_line; std::vector<point<double> > down_line; for (int i=0; i<up_num; ++i){ int index = i*3; point<double> p(*(up_data+index), *(up_data+index+1), *(up_data+index+2) ); up_line.push_back(p); } for (int i=0; i<up_num; ++i){ int index = i*3; point<double> p(*(down_data+index), *(down_data+index+1), *(down_data+index+2) ); down_line.push_back(p); } boundary_point bound(up_line, down_line); bound.cacu(); std::vector<point<double> > val = bound.gen_inter_line(ratio); int size = (int)val.size(); int located = size*3; double *r_val = new double[located]; for (int i = 0; i<size; i++){ int index = i*3; *(r_val+index+0) = val[i].coord[0]; *(r_val+index+1) = val[i].coord[1]; *(r_val+index+2) = val[i].coord[2]; } *return_num = size; return r_val; } void del_inter_line(double * r_val) { delete [] r_val; }

May somebody give me some guide or wise recommendation. thanks.


In reply to xs memoryleak by yarp

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.