There are a couple of obvious causes for a leak.
The one that is surely a leak is the js
object that you create but fail to destroy. Protect its lifetime with a try
/finally
block:
js := TlkJSON.ParseText( mystream.DataString ) as TlkJsonObject;
try
...
finally
js.Free;
end;
More subtle is the un-protected mystream
. If an exception is raised after mystream
is assigned, and before it is destroyed, you will leak that object. Again you should protect that with a try
/finally
block using the exact same idiom as demonstrated above. The problem with mystream
will only be an issue for you in case your function is raising exceptions. Whilst that might not be the case here you must always protect objects in the manner shown above.
In any event, you should certainly do some leak tracking in your program. There may very well be more leaks. Here are the first steps you need to take:
- Change your code so that it uses the full version of FastMM.
- Set up the FastMM configuration file for full debug. That may involve editing the .inc file and if you find that too daunting, you can use the GUI utility from here: http://jed-software.com/blog/?p=606
- Run your program for a little while and then close it. If you have leaks these should be reported at this point.
- Fix all your leaks.
It is possible that you have leaks during the running of your program, but that are all tidied up when the program closes. These are harder to track and require some extra instrumentation of your program.