Externals and Memory Leaks???

Dave dave at looktowindward.com
Thu May 24 13:58:34 EDT 2007


Hi Again,

Thanks a lot!  Here is my whole function again with the added "free"  
is this what you mean?

All the Best
Dave


OSStatus LTWLibraryUtilsSetExternalVariablePtr(	char*				 
theVariableNamePtr,
												const char*			theVariablePtr,
												long				theVariableSize)
{
ExternalString				myExternalVariableInfo;
int							myRunRevStatus;

//**
//**	Get the External Variable
//**
GetVariableEx 
(theVariableNamePtr,"",&myExternalVariableInfo,&myRunRevStatus);
if (myRunRevStatus == EXTERNAL_FAILURE)
	return(kRunRevVariableNotDefined);

//**
//**	Get the External Variable
//**
myExternalVariableInfo.buffer = theVariablePtr;
myExternalVariableInfo.length = theVariableSize;
SetVariableEx 
(theVariableNamePtr,"",&myExternalVariableInfo,&myRunRevStatus);
if (myRunRevStatus == EXTERNAL_FAILURE)
	return(kRunRevVariableNotDefined);
if (theVariablePtr != null) 	
	free(theVariablePtr);
	
//**
//**	Good Return
//**
return(noErr);
}


On 24 May 2007, at 18:18, chris bohnert wrote:

> Dave,
>
> To my knowledge, Rev. makes a copy of the buffer when you do the  
> SetVariable
> call.  So in instances where you're making successive calls like  
> this you
> should never expect rev to send back a ptr that you've already
> allocated..its probably safe enough to do something like this in your
> external code
>
> myExternalVariableInfo.buffer = theVariablePtr;
> myExternalVariableInfo.length = theVariableSize;
> SetVariableEx
> (theVariableNamePtr,"",&myExternalVariableInfo,&myRunRevStatus);
> if (myRunRevStatus == EXTERNAL_FAILURE)
>       return(kRunRevVariableNotDefined);
>
> // added explicit free here as rev should have copied the buff in the
> SetVariable Call
> if( theVariable!=NULL)
>  free theVariable;
>
>
> In instances where you have allocated memory that needs to be  
> persisted over
> a long period of time in the external ( image buffer, multiple  
> instances of
> browser object and the like) you'll want to track them similar to  
> previous
> description.
>
> --
> cb
>
> On 5/24/07, Dave <dave at looktowindward.com> wrote:
>>
>> Hi Chris,
>>
>> Thanks for the information, but I'm a bit confused! In the RunRev
>> script I have the following code:
>>
>> local myBuffer
>> local myNewData
>>
>> repeat until <some_condition>
>>   get XGetData("myTempData")
>> XProcessData(myTempData,"myNewData")
>> end repeat
>>
>> In my externals I allocate a new buffer each time it's called, should
>> I check if the variable already has something allocated to it and
>> free it if so? I am guessing this is what you mean when you say store
>> the Allocations in a list? So before allocating a new buffer check if
>> the variable has a buffer assigned and then free it if (and only if)
>> it's in the list?
>>
>> Thanks again
>> All the Best
>> Dave
>>
>> On 24 May 2007, at 15:58, chris bohnert wrote:
>>
>> > Dave,
>> >
>> > Yep..when you use SetVariableEx you're responsible for managing the
>> > memory.
>> > Normally I handle this by building a SafeAlloc function in C or by
>> > creating
>> > my own  new/delete in c++.
>> >
>> > This will let you store your allocs in a list and handle their
>> > release as
>> > necessary.  In my experience, releasing only what I know I have
>> > allocated is
>> > much safer than trying to free random memory blocks coming back
>> > from rev.
>> >
>> > hope that helps.
>> >
>> > --
>> > cb
>> >
>> > On 5/24/07, Dave < dave at looktowindward.com> wrote:
>> >>
>> >> Hi All,
>> >>
>> >> I have a Stack that uses two functions from an external:
>> >>
>> >> XGetData("myTempData")
>> >> XProcessData(myTempData,"myNewData")
>> >>
>> >> XGetData() Gets the data into the correct format and stores it  
>> in the
>> >> RunRev variable "myTempData".
>> >>
>> >> XProcessData() takes the data in "myTempData", processes it and  
>> puts
>> >> the results into "myNewData".
>> >>
>> >> This all works well and produces good results, the only problem is
>> >> that memory seems to be leaking by quite a lot and the two data
>> >> buffers here are quite large.
>> >>
>> >> In the external, I use malloc() to allocate the buffers and  
>> then use
>> >> "SetVariableEx()" to pass it back to RunRev. My question is,  
>> who is
>> >> responsible for freeing the data?
>> >>
>> >> I am guessing that the on each subsequent call to the External
>> >> Functions causes a new block to be allocated and overwrites the
>> >> contents of the old RunRev "ExternalString" structure without  
>> first
>> >> freeing it. How should I handle this? Do I need to check if the
>> >> variable passed into my external already has data allocated and  
>> if so
>> >> either free or re-use the data (the block sizes are likely to  
>> be the
>> >> same).
>> >>
>> >> Any advice or help on this greatly appreciated.
>> >>
>> >> All the Best
>> >> Dave
>> >>
>> >> PS
>> >>
>> >> I set the External Variable using the following function:
>> >>
>> >>
>> >> OSStatus LTWLibraryUtilsSetExternalVariablePtr( char*
>> >> theVariableNamePtr,
>> >>
>> >>                            const
>> >> char*                     theVariablePtr,
>> >>
>> >>
>> >>                            long
>> >> theVariableSize)
>> >> {
>> >> ExternalString                          myExternalVariableInfo;
>> >> int
>> >> myRunRevStatus;
>> >>
>> >> //**
>> >> //**    Get the External Variable
>> >> //**
>> >> myExternalVariableInfo.buffer = theVariablePtr;
>> >> myExternalVariableInfo.length = theVariableSize;
>> >> SetVariableEx
>> >> (theVariableNamePtr,"",&myExternalVariableInfo,&myRunRevStatus);
>> >> if (myRunRevStatus == EXTERNAL_FAILURE)
>> >>         return(kRunRevVariableNotDefined);
>> >>
>> >> //**
>> >> //**    Good Return
>> >> //**
>> >> return(noErr);




More information about the use-livecode mailing list