tolua potential memory leak?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

tolua potential memory leak?

Fabian Peña
please see this C code


const char * getSome()
{

   return strdup("some string");
}


tolua generated code


static int tolua_getSome(lua_State* tolua_S)
{

// bla bla bla
     const char* tolua_ret = (const char*)  getSome();
    tolua_pushstring(tolua_S,(const char*)tolua_ret);
   return 1;
}

Tthat it  happens to the memory assigned in strdup?   Memory Leak ?






Reply | Threaded
Open this post in threaded view
|

Re: tolua potential memory leak?

Fabian Peña
And ..

char * getFile()
{
   char *buffer;
     FILE *f;
   f=fopen("somefile","rb");
   //bla bla
   fread((char *)buffer,lengthoffile,1);
   fclose(f);
   return buffer;
}

Use this function in c

 char *buffer;
 buffer = getFile();

//Do something with the buffer

 free(buffer);

//be happy






Fabian Peña wrote:

please see this C code


const char * getSome()
{

   return strdup("some string");
}


tolua generated code


static int tolua_getSome(lua_State* tolua_S)
{

// bla bla bla
     const char* tolua_ret = (const char*)  getSome();
    tolua_pushstring(tolua_S,(const char*)tolua_ret);
   return 1;
}

Tthat it  happens to the memory assigned in strdup?   Memory Leak ?








Reply | Threaded
Open this post in threaded view
|

Re: tolua potential memory leak?

Brian Weed-2
In reply to this post by Fabian Peña
Fabian Peña wrote:

please see this C code


const char * getSome()
{

   return strdup("some string");
}


tolua generated code


static int tolua_getSome(lua_State* tolua_S)
{

// bla bla bla
     const char* tolua_ret = (const char*)  getSome();
    tolua_pushstring(tolua_S,(const char*)tolua_ret);
   return 1;
}

Tthat it  happens to the memory assigned in strdup?   Memory Leak ?

Aren't you responsible for freeing the string returned by strdup() ?

The following change would avoid the leak:

const char * getSome()
{
   return "some string";
}

But I assume you can't do that for some reason, or you would not be asking.

Brian



Reply | Threaded
Open this post in threaded view
|

Re: tolua potential memory leak?

Alex Sandro Queiroz e Silva-2
Hallo,

Brian Weed wrote:

The following change would avoid the leak:

const char * getSome()
{
   return "some string";
}


The address of the string would be meaningless after the return of this function, wouldn't it?

-alex
(http://www.ventonegro.org/)




Reply | Threaded
Open this post in threaded view
|

Re: tolua potential memory leak?

David Given
In reply to this post by Fabian Peña
On Thursday 24 February 2005 17:07, Fabian PeÃa wrote:
> please see this C code
[...]
> Tthat it  happens to the memory assigned in strdup?   Memory Leak ?

Yes.

lua_pushstring takes its own copy of the string, which means that it's your 
responsibility to free the string returned by getSome() once you've done with 
it.

int tolua_getSome(lua_State* L)
{
 const char* s = getSome();
 lua_pushstring(L, s);
 free(s);
 return 1;
};

(BTW, I notice your code calls tolua_pushstring(), not lua_pushstring()... I 
don't know whether that's a typo or if you're using the tolua library, which 
I have no experience of. If so, tolua_pushstring() might have different 
semantics.)

-- 
"Curses! Foiled by the chilled dairy treats of righteousness!" --- Earthworm 
Jim (evil)


Reply | Threaded
Open this post in threaded view
|

Re: tolua potential memory leak?

Ben Sunshine-Hill
In reply to this post by Alex Sandro Queiroz e Silva-2
On Thu, 24 Feb 2005 17:07:52 -0300, Alex Sandro Queiroz e Silva
<[hidden email]> wrote:
> Brian Weed wrote:
> >
> > const char * getSome()
> > {
> >    return "some string";
> > }
> 
>     The address of the string would be meaningless after the return of
> this function, wouldn't it?

No... string literals aren't on the stack, so the pointer will remain
valid. (Not sure if this is specified by the standard, or just the way
everyone does it.)

Reply | Threaded
Open this post in threaded view
|

Re: tolua potential memory leak?

Adriano Ferreira
In reply to this post by Alex Sandro Queiroz e Silva-2
On Thu, 24 Feb 2005 17:07:52 -0300, Alex Sandro Queiroz e Silva >    
The address of the string would be meaningless after the return of
> this function, wouldn't it?

I don't think so. The literal "some string" in C code must be treated
as a constant char array with a valid address. The compiler will
usually save it somewhere with a well defined memory space. But it
should not be changed in general. Or you risk seeing other contents
when passing through it again, besides the possible buffer overflow
hazzards if the original length is not respected when it is modified.

Regards,
Adriano.