wallet-core icon indicating copy to clipboard operation
wallet-core copied to clipboard

Improves memory handling in TWAnyAddress

Open optout21 opened this issue 3 years ago • 1 comments

Describe the bug Not really a bug: create a class to automatically handle deallocation of TWAnyAddress object.

Additional context https://github.com/trustwallet/wallet-core/blob/master/src/interface/TWAnyAddress.cpp#L16

struct TWAnyAddress {
    TWString* address;
    enum TWCoinType coin;
};

void TWAnyAddressDelete(struct TWAnyAddress* _Nonnull address) {
    TWStringDelete(address->address);
    delete address;
}

optout21 avatar Jul 29 '22 20:07 optout21

I think I misunderstood the real issue here. Here's how I think it should be:

  • There is a TWAnyAddress struct holding an address (incl. coin) ☑️
  • Internals of TWAnyAddress should be hidden, should not be visible in the .h file ❗️
  • TWAnyAddress can can be created from address string or pubkey ☑️
  • Methods to obtain address string / coin from TWAnyAddress (TWAnyAddressDescription, TWAnyAddressCoin) ☑️
  • TWAnyAddressDelete to clean up address object when no longer needed ☑️
  • Since internals is not visible to outside, std::string can be used to store the string inside the struct. ❗️

So the proposed changes:

  • Move definition of internals of TWAnyAddress to the .cpp file
  • Change the type of address field from TWString* to std::string

optout21 avatar Aug 12 '22 08:08 optout21