3

I have some code that loops and news up some pointers and stores them in a vector:

std::vector<InputBox*> m_octets; 
...  
InputBox* octet = new InputBox(rect, title, touch_num);
m_octets.push_back(octet);

In the class destructor I for_each over m_octets and invoke the destructor for each pointer. I think this is all good. It all compiles and the unit tests pass. The problem is Gimpel's PC-lint doesn't like it. It sees that `octet' is a custodial pointer that has not been freed (Warning 429). I can of course disable that warning but the manual (11.2.1) indicates there is a semantic for this. I would have thought would work:

-sem(*push_back, custodial (1))

Unfortunately it has no effect. I've tried various combinations including fully specifying m_octets.push_back but nothing seems to work. Does anybody know the proper form of this command for the example given?

cafce25
  • 15,907
  • 4
  • 25
  • 31
Tod
  • 8,192
  • 5
  • 52
  • 93
  • 1
    It might be recognizing that the pointers' memory leaks if an exception is thrown, since the code you posted is not exception safe. You can fix this by using unique_ptr instead of a raw pointer, or if that isn't available, using a smart container that deletes the pointers it holds in its destructor. – Dirk Holsopple Aug 22 '12 at 19:41
  • @DirkHolsopple No, unfortunately PClint is not so smart :-( – Rost Aug 22 '12 at 19:59
  • @Dirk thanks for the heads up. Using a smart container is something I will look into in the future. I tend not to worry about exception safety because almost all my C++ code is for embedded development and exception handling is typically disabled (`new` is really `new (nothrow)`). I tend to write the code such that all dynamic memory is allocated on start up. Typically the destructors won't even ever be invoked but I'm trying to make it as clean as possible. – Tod Aug 22 '12 at 20:26

1 Answers1

2

This one works fine for me: -sem(std::vector::push_back, custodial(1))

Rost
  • 8,779
  • 28
  • 50
  • I don't understand why *push_back doesn't work but when I fully specified it as shown in your post it worked for me. Thanks. – Tod Aug 22 '12 at 20:16
  • 1
    @Tod Looks like wildcards don't work for semantics, only exact match. – Rost Aug 22 '12 at 20:28
  • At which position in the code do you paste that Lint comment to make it work? Just before the call? Before the method containing the call? Before the class declaration? – Strubbl Jan 22 '13 at 14:48
  • 1
    @Strubbl I used `.lnt` file, not comment. Comment shall be added before vector object definition. – Rost Jan 22 '13 at 15:21