Hi Monks,

I am facing a problem to delete memory which is allocated using Nexz function. Here is my sample programme, in my sample programme I want to concatenate two strings and show the result.
First .h file (StringTest.h)
===========================

#include <stdio.h> #include <stdlib.h> #include <string.h> char* StringAdd (char* x, char* y); void FreeMemory (void* z);
.c File (StringTest.c)
======================
#include "StringTest.h" char* StringAdd (char* x, char* y) { char* ReturnString; int Length = strlen(x) + strlen(y); ReturnString = (char*) malloc (Length*sizeof(char)); strcpy (ReturnString, x); strcat (ReturnString, y); return ReturnString; } void FreeMemory (void* z) { void* current = NULL; current = z; free (current); current = NULL; if (NULL == current ) { printf ("Memory deallocated\n"); } }
.xs file (MyStringTest.xs)
===========================
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #define XARG 123 #include "StringTest.h" MODULE = MyStringTest PACKAGE = MyStringTest char* TestStringAdd (a,b) char* a char* b CODE: char* c; char* d; int length; c = StringAdd(a, b); length = strlen(c); Newz (XARG, d, length, char*); strcpy (d, (const char*)c); // deallocate Memory FreeMemory (c); printf ("Name::%s\n", d); printf ("Length::%d\n", length); RETVAL = d; OUTPUT: RETVAL
Test file (test.pl)
========================
use Test; BEGIN { plan tests => 1 }; use MyStringTest; print MyStringTest::TestStringAdd(Pijush, Koley), "\n"; ok(1); # If we made it this far, we're ok.
The code is working fine. But there is memory leakage problem in the code. I am doing a Newz, but no corresponding Safefree is there. I want to avoid the memory leakage problem. I know if I rewrite the programme in OO model, then in DESTROY method I can call Safefree function. Can you please suggest me any other method so that I do not need to rewrite the complete function?
Thanks in advance.
-Pijush

In reply to Help needed for Perl XS code by pijush

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.