How do i return an object from a function in Delphi without causing Access Violation?
As Smasher said, you can't free it; the code calling the function that returns the object is responsible for destroying it.
This is bad code design, by the way, as it makes it confusing as to who allocates and frees. A much better way to do it would be to have the caller create the object and pass it in to the function. That way, the code that creates it also frees it. Something like this:
var
SL: TStringList;
begin
SL := TStringList.Create;
try
ProcToFillStringList(SL);
//Do something with populated list
finally
SL.Free;
end;
end;
// Note I've made the parameter a TStrings and not a TStringList. This allows
// passing a TMemo.Lines or a TListBox or TComboBox Items as well.
procedure ProcToFillStringList(const SList: TStrings);
// Do whatever populates the list with SList.Add()
end;
Now there's no confusion over who does what - the same code that creates the object is responsible for freeing it. And the code, IMO, is much clearer to read and maintain.
How can i return the TStringList and still free it in the local function?
You can't. If you free it in the local function, you can't use the return value. Result and vStrList point to the same TStringList object in memory. TStringList is a class and
Result := vStrList
does therefore not copy the string list, but only copies the reference.
So, instead you should free the string list in the calling context after you're done working with it or pass the string list as a parameter to your function like this
procedure FuncStringList (StringList : TStringList);
and let the calling code create and free the string list. As pointed out by the other answers, this is the preferable way, since it makes ownership very clear.
Simple answer: you can't. Why are you trying to? Is it because you've learned that you need to free every object you create in the same function in which they're created? That's generally correct, but not always, and this is one of the exceptions to the rule. A better way to put it is that every object must be freed by its owner.
If you have a function that generates an object, like this one, but then passes it on to another function, it doesn't take ownership of the object. Remove the call to free and document it, so you (and anyone else who uses this function) will realize that it creates a new object that the code that calls it has to take ownership of.