Better HN
topnewbestaskshowjobs
Log inSign up

cthrowappp

5 karma · joined Oct 10, 2015 (11y ago) · 4 submissions

Submissions

comment 11y ago · link · parent

The headline has since been fixed to mention Clang or GCC with Blocks.

comment 11y ago · link · parent

That's pretty cute.

comment 11y ago · link · parent

Not in this case it doesn't.

comment 11y ago · link · parent

Unless I'm mistaken, `__attribute__((cleanup))` is function-, not block-, scoped. The cleanup attribute is actually declared on the Block, which is kind of cute.

Instead, I'd suggest just making regular use of `__attribute__((cleanup))` in C under GCC or Clang and avoiding C blocks entirely. In this case you could do something like:

  #define AUTOCLOSE_FILE(n) __attribute__((cleanup(autoclose_file))) n = NULL
  
  void
  autoclose_file(FILE *f)
  {
    if (f)
      fclose(f);
  }
  
  int
  main(...)
  {
    FILE AUTOCLOSE_FILE(*a), AUTOCLOSE_FILE(*b);
  
    a = fopen("foo");
    if (!a)
      return;

    b = fopen("bar");
    if (!b)
      return;
  
    ...
  }