@@ -133,7 +133,7 @@ func SetLogMsgLevel(level LogMsgLevel) {
133133// ReadMetadata reads the metadata of an Image
134134func (i * Image ) ReadMetadata () error {
135135 if i .img == nil {
136- return errors .New ("image is nil" )
136+ return errors .New ("image instance is not initialized: underlying C structure is nil" )
137137 }
138138
139139 var cerr * C.Exiv2Error
@@ -164,7 +164,9 @@ func (i *Image) GetBytes() []byte {
164164 }
165165
166166 result := C .GoBytes (unsafe .Pointer (ptr ), C .int (size ))
167+
167168 runtime .KeepAlive (i ) // Prevent GC from freeing the C structure prematurely
169+
168170 return result
169171}
170172
@@ -174,6 +176,7 @@ func (i *Image) PixelWidth() int64 {
174176 return 0
175177 }
176178 result := int64 (C .exiv2_image_get_pixel_width (i .img ))
179+
177180 runtime .KeepAlive (i )
178181
179182 return result
@@ -185,6 +188,7 @@ func (i *Image) PixelHeight() int64 {
185188 return 0
186189 }
187190 result := int64 (C .exiv2_image_get_pixel_height (i .img ))
191+
188192 runtime .KeepAlive (i )
189193
190194 return result
@@ -201,6 +205,7 @@ func (i *Image) ICCProfile() []byte {
201205 return nil
202206 }
203207 result := C .GoBytes (unsafe .Pointer (C .exiv2_image_icc_profile (i .img )), size )
208+
204209 runtime .KeepAlive (i )
205210
206211 return result
@@ -209,7 +214,7 @@ func (i *Image) ICCProfile() []byte {
209214// SetMetadataString sets an exif or iptc key with a given string value
210215func (i * Image ) SetMetadataString (format , key , value string ) error {
211216 if i .img == nil {
212- return errors .New ("image is nil" )
217+ return errors .New ("image instance is not initialized: underlying C structure is nil" )
213218 }
214219
215220 if format != "iptc" && format != "exif" {
@@ -244,7 +249,7 @@ func (i *Image) SetMetadataString(format, key, value string) error {
244249// SetMetadataShort sets an exif or iptc key with a given short value
245250func (i * Image ) SetMetadataShort (format , key , value string ) error {
246251 if i .img == nil {
247- return errors .New ("image is nil" )
252+ return errors .New ("image instance is not initialized: underlying C structure is nil" )
248253 }
249254
250255 if format != "iptc" && format != "exif" {
@@ -278,7 +283,7 @@ func (i *Image) SetMetadataShort(format, key, value string) error {
278283
279284func (i * Image ) StripKey (f MetadataFormat , key string ) error {
280285 if i .img == nil {
281- return errors .New ("image is nil" )
286+ return errors .New ("image instance is not initialized: underlying C structure is nil" )
282287 }
283288
284289 ckey := C .CString (key )
0 commit comments