Page MenuHomePhabricator

Fix small Mac OSX memory leak (imported from BU sources)
ClosedPublic

Authored by CCulianu on Aug 8 2017, 22:47.

Details

Summary

I noticed there was a very small memory leak in the way CoreFoundation objects were being used on Mac OSX. I submitted that patch months ago to the BU team to fix it, and it's been accepted and merged into their repository. I wanted to bring that patch here to our project, just as a small nit, because it bugs me the way Core originally wrote this code. They clearly don't know how to use the CoreFoundation API ;)

Here is the reference in the BU repository:

https://github.com/BitcoinUnlimited/BitcoinUnlimited/pull/507/commits/045a999785d213a2366158fc4372f51041800e7a

Test Plan

Compile on OSX if you dare, open Qt gui app, toggle load on startup on/off

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

CCulianu retitled this revision from Fix small Mac OSX Qt memory leak fix (imported from BU sources) to Fix small Mac OSX memory leak (imported from BU sources).Aug 8 2017, 22:49
CCulianu edited the summary of this revision. (Show Details)

Seems ok. I have no way of testing this, but the changes do match earlier BU commit 045a999785d .

src/qt/guiutil.cpp
798 ↗(On Diff #1122)

A cast to bool would seem clearer to me than this idiom.

Adding minor comment to test if I have subscriber/review access.

src/qt/guiutil.cpp
795 ↗(On Diff #1122)

nitpick: add braces and newline for style consistency.

CCulianu added inline comments.
src/qt/guiutil.cpp
795 ↗(On Diff #1122)

roger that

798 ↗(On Diff #1122)

yeah that was core's idiom. really can just return foundItem. No need for the !!

src/qt/guiutil.cpp
798 ↗(On Diff #1122)

Given that foundItem's implicit cast to bool is already used in many other spots in the added code, it makes sense to just return foundItem for consistency.

Added changes as per reviewer comments

Oops, missed a set of braces

FYI since you guys don't have MacOS -- here is proof that this thing builds on Mac:

Screenshot on Mac:

Screen Shot 2017-08-09 at 9.36.29 PM.png (1×1 px, 316 KB)

Compile log:

Options used to compile and link:
  with wallet   = yes
  with gui / qt = yes
    qt version  = 5
    with qr     = yes
  with zmq      = yes
  with test     = yes
  with bench    = yes
  with upnp     = auto
  debug enabled = no
  werror        = no

  target os     = darwin
  build os      = darwin

  CC            = gcc
  CFLAGS        = -g -O2
  CPPFLAGS      =  -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS -isystem /opt/local/include -I/opt/local/include/db48 -DMAC_OSX
  CXX           = g++ -std=c++11
  CXXFLAGS      = -g -O2 -Wall -Wextra -Wformat -Wvla -Wformat-security -Wno-unused-parameter -Wno-self-assign -Wno-unused-local-typedef -Wno-deprecated-register
  LDFLAGS       =  -Wl,-headerpad_max_install_names -Wl,-dead_strip

calin@CalinMBP2016 ~/tmp/bitcoin-abc $ make -j 6
Making all in src
  CXX      libbitcoinconsensus_la-arith_uint256.lo
  CXX      libbitcoinconsensus_la-hash.lo
  CXX      libbitcoinconsensus_la-pubkey.lo
  CXX      libbitcoinconsensus_la-uint256.lo
  CXX      libbitcoinconsensus_la-utilstrencodings.lo
gcc -I. -g -O2 -Wall -Wextra -Wno-unused-function -c src/gen_context.c -o gen_context.o
  CXX      bitcoind-bitcoind.o
  CXX      libbitcoin_server_a-addrman.o
  CXX      libbitcoin_server_a-addrdb.o
  CXX      libbitcoin_server_a-bloom.o
gcc gen_context.o -o gen_context
./gen_context
  CC       src/libsecp256k1_la-secp256k1.lo
  CXX      libbitcoin_server_a-blockencodings.o
  CXX      libbitcoin_server_a-chain.o
... LOTS OF COMPILING ...

  CXX      qt/qt_libbitcoinqt_a-csvmodelwriter.o
  CXX      qt/qt_libbitcoinqt_a-guiutil.o         <--- this is the file I touched.. it compiled ok.
  CXX      qt/qt_libbitcoinqt_a-intro.o
... LOTS MORE COMPILING ...
CXXLD    test/test_bitcoin_fuzzy
  CXXLD    bitcoind
  CXXLD    test/test_bitcoin
  CXXLD    bench/bench_bitcoin
  AR       qt/libbitcoinqt.a
  OBJCXXLD qt/bitcoin-qt
  CXXLD    qt/test/test_bitcoin-qt
Making all in doc/man
make[1]: Nothing to be done for `all'.
make[1]: Nothing to be done for `all-am'.
calin@CalinMBP2016 ~/tmp/bitcoin-abc $

Nice, thanks. Accepted on code review basis, @CCulianu you are responsible if this breaks Mac users :-)

This revision is now accepted and ready to land.Aug 9 2017, 18:56

Nice, thanks. Accepted on code review basis, @CCulianu you are responsible if this breaks Mac users :-)

Ha ha :)

Dude I'm the Mac Master. Trust me.. it doesn't. I know Mac API very well. I have written many native macOS apps. Some are in the macOS app store!

Core's use of the Mac API was very bad -- they allocated/reference counted a bunch of objects then never release them. Bad bad bad...

This revision was automatically updated to reflect the committed changes.