Check if property exists before accessing or setting it

Previously when accessing/setting a property which does not exist,
there will be segmentation faults
This commit is contained in:
Stephan Sundermann 2013-10-10 20:26:26 +02:00
parent 35a9c16ea7
commit 4d0a5a796b

View file

@ -13,23 +13,43 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
namespace Gst {
using System;
using System.Runtime.InteropServices;
using System;
using System.Runtime.InteropServices;
namespace Gst {
public class PropertyNotFoundException : Exception {}
partial class Object
{
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr g_object_class_find_property (IntPtr klass, IntPtr name);
bool PropertyExists (string name) {
var ptr = g_object_class_find_property (GType.GetClassPtr (), GLib.Marshaller.StringToPtrGStrdup (name));
var result = ptr != IntPtr.Zero;
GLib.Marshaller.Free (ptr);
return result;
}
public object this[string property] {
get {
GLib.Value v = GetProperty (property);
object o = v.Val;
v.Dispose ();
return o;
if (PropertyExists (property)) {
GLib.Value v = GetProperty (property);
object o = v.Val;
v.Dispose ();
return o;
} else
throw new PropertyNotFoundException ();
} set {
GLib.Value v = new GLib.Value (this, property);
v.Val = value;
SetProperty (property, v);
v.Dispose ();
if (PropertyExists (property)) {
GLib.Value v = new GLib.Value (this, property);
v.Val = value;
SetProperty (property, v);
v.Dispose ();
} else
throw new PropertyNotFoundException ();
}
}