Fix macOS memory leaks, also avoid false positive warnings from clang analyzer

__clang_analyzer__ is a constant that only is defined during analyze build, this helps avoiding false positives as long as there is no specific way to silence analyzer messages
This commit is contained in:
Stefan Csomor
2019-04-21 23:52:37 +02:00
parent d0a84a6266
commit 5020a810db
7 changed files with 143 additions and 47 deletions

View File

@@ -1013,6 +1013,83 @@ void wxMacCocoaRelease( void* obj );
void wxMacCocoaAutorelease( void* obj );
void* wxMacCocoaRetain( void* obj );
// shared_ptr like API for NSObject and subclasses
template <class T>
class wxNSObjRef
{
public:
typedef T element_type;
wxNSObjRef()
: m_ptr(NULL)
{
}
wxNSObjRef( T p )
: m_ptr(p)
{
}
wxNSObjRef( const wxNSObjRef& otherRef )
: m_ptr(wxMacCocoaRetain(otherRef.m_ptr))
{
}
wxNSObjRef& operator=( const wxNSObjRef& otherRef )
{
if (this != &otherRef)
{
wxMacCocoaRetain(otherRef.m_ptr);
wxMacCocoaRelease(m_ptr);
m_ptr = otherRef.m_ptr;
}
return *this;
}
wxNSObjRef& operator=( T ptr )
{
if (get() != ptr)
{
wxMacCocoaRetain(ptr);
wxMacCocoaRelease(m_ptr);
m_ptr = ptr;
}
return *this;
}
T get() const
{
return m_ptr;
}
operator T() const
{
return m_ptr;
}
T operator->() const
{
return m_ptr;
}
void reset( T p = NULL )
{
wxMacCocoaRelease(m_ptr);
m_ptr = p; // Automatic conversion should occur
}
// Release the pointer, i.e. give up its ownership.
T release()
{
T p = m_ptr;
m_ptr = NULL;
return p;
}
protected:
T m_ptr;
};
#endif
// _WX_PRIVATE_CORE_H_