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:
@@ -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_
|
||||
|
Reference in New Issue
Block a user