Cannot pass GLib.List <string> as an argument (error: duplicating List instance, use unowned variable or explicitly invoke copy method)
What is problem.
I got an below error.
../src/parser/CountLineOfCode.vala:34.27-34.38: error: duplicating List instance, use unowned variable or explicitly invoke copy method
public void cloc (List<string> list) {
^^^^^^^^^^^^
So, I check a similar problem and tried to solve it, but I couldn't.
I think the ownership of the variable is incorrect.
When verified with a small code, however, the same error did not occurs.
Success Case
In case of success, error does not occurs.
/* Application.vala */
namespace Camilla {
public class Application : GLib.Application {
public static int main (string[] args) {
Run run = new Run ();
run.run ();
return 0;
}
}
}
/* Run.vala */
namespace Camilla {
public class Run : GLib.Object {
GLib.List<string> list;
public void run () {
list = new List<string> ();
list.append ("aaa");
list.append ("bbb");
Ok ok = new Ok ();
ok.printList (list);
}
}
}
/* Ok.vala */
namespace Camilla {
public class Ok : GLib.Object {
public void printList (List<string> list) {
foreach (weak string str in list) {
stdout.printf ("%s\n", str);
}
}
}
}
Failure Case
In this case, the cloc method of the CountLineOfCode class will result in the error.
Only the relevant parts are shown below.
/* Application.vala */
namespace Camilla {
public class Application : GLib.Application {
public static int main (string[] args) {
Camilla camilla = new Camilla ();
return camilla.run (args);
}
}
}
/* Camilla.vala */
namespace Camilla {
public class Camilla : GLib.Object {
/** File list to be checked. */
private List<string> targetFileList;
public int run (string[] args) {
/* walk () returns files under the current directory as GLib.List<string> */
targetFileList = Core.File.walk ("."); /* return list with copy_deep(strdup) [Camilla.vala](/uploads/ec189b4b1ace296fdac31f4ed7372a82/Camilla.vala)
[Application.vala](/uploads/ea2b349a98f60b91493600d7815c1331/Application.vala)[CountLineOfCode.vala](/uploads/ee6606ebf5203a77dc5cc2d72f891698/CountLineOfCode.vala)*/
parse ();
return EXIT_STATUS.SUCCESS;
}
private void parse () {
if (argParser.hasOption ("c")) {
CountLineOfCode cloc = new CountLineOfCode ();
cloc.cloc (targetFileList); /* Error point. */
return;
}
}
}
}
/* CountLineOfCode.vala */
namespace Camilla.Parser {
public class CountLineOfCode : GLib.Object {
public void cloc (List<string> list) { /* Error point. */
/* Omit */
}
}
}
To remove the error, I changed method definition like that cloc (owned List<string> list)
.
After that, I changed caller like that cloc.cloc (targetFileList.copy_deep (strdup));
However, error still occurs.
In the case of unowned, that is cloc (List<weak string> list)
and cloc.cloc (targetFileList.copy());
,
same error occurs.