Externals and Memory Leaks???
chris bohnert
chris at altuit.com
Thu May 24 10:58:27 EDT 2007
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);
> }
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
>
>
More information about the use-livecode
mailing list