'static needed, leaking causes .. well, leak

I'm calling clap's Arg::default_value() to set an argument's default value. The default value needs to be configurable at runtime (it a String that is derived from the successful output of std::env::current_exe()), but default_value(), indirectly, takes in a &'static str.

Leaking the String solves the lifetime constraint issue, but causes our CI to report a memory leak.

  1. Is there anything I'm missing with clap that would allow a default value that does not require 'static?
  2. Is there a way to avoid the leak? (Let's assume that once the argument parser has been dropped, the buffer can be released).

I'm not too familiar with clap, but it looks like you can discover where an argument value came from. So, you could put an empty string as the default value and then replace it with the dynamically-generated one iff it came from the default.


On the other hand, it sounds to me like this case isn't an actual problem, but rather an overeager CI check that prevents a legitimate technique. You could relax the CI's requirements instead.

You can create a static once_cell::sync::Lazy<String> which can in turn be borrowed as a &'static str. Does you CI accept that?

2 Likes

Enable the string feature of clap. Then, you should be able to pass String.

4 Likes

you can enable the string feature of clap, and just pass a String to it.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.