@@ -110,12 +110,17 @@ private static void StashPopImpl()
110110 var formatter = CreateFormatter ( ) ;
111111 var storage = ( RuntimeDataStorage ) formatter . Deserialize ( ms ) ;
112112
113- StashPopObjects ( storage . GetStorage ( "objs" ) ) ;
113+ var objs = StashPopObjects ( storage . GetStorage ( "objs" ) ) ;
114114 StashPopModules ( storage . GetStorage ( "modules" ) ) ;
115115 ClassManager . StashPop ( storage . GetStorage ( "classes" ) ) ;
116116 TypeManager . StashPop ( storage . GetStorage ( "types" ) ) ;
117117 ImportHook . StashPop ( storage . GetStorage ( "import" ) ) ;
118118 PyCLRMetaType = MetaType . StashPop ( storage . GetStorage ( "meta" ) ) ;
119+
120+ foreach ( var item in objs )
121+ {
122+ XDecref ( item . pyHandle ) ;
123+ }
119124 }
120125
121126 public static bool HasStashData ( )
@@ -137,6 +142,7 @@ private static void StashPushObjects(RuntimeDataStorage storage)
137142 foreach ( var entry in objs )
138143 {
139144 var obj = entry . Key ;
145+ XIncref ( obj . pyHandle ) ;
140146 switch ( entry . Value )
141147 {
142148 case ManagedType . TrackTypes . Extension :
@@ -190,6 +196,7 @@ private static void StashPushObjects(RuntimeDataStorage storage)
190196 }
191197 foreach ( var clrObj in wrappers [ item . Instance ] )
192198 {
199+ XIncref ( clrObj . pyHandle ) ;
193200 clrObj . Save ( ) ;
194201 }
195202 }
@@ -198,13 +205,15 @@ private static void StashPushObjects(RuntimeDataStorage storage)
198205 storage . AddValue ( "wrappers" , wrapperStorage ) ;
199206 }
200207
201- private static void StashPopObjects ( RuntimeDataStorage storage )
208+ private static IEnumerable < ManagedType > StashPopObjects ( RuntimeDataStorage storage )
202209 {
203210 var extensions = storage . GetValue < List < ManagedType > > ( "extensions" ) ;
204211 var internalStores = storage . GetValue < List < CLRObject > > ( "internalStores" ) ;
212+ var storedObjs = new List < ManagedType > ( ) ;
205213 foreach ( var obj in Enumerable . Union ( extensions , internalStores ) )
206214 {
207215 obj . Load ( ) ;
216+ storedObjs . Add ( obj ) ;
208217 }
209218 if ( WrappersStorer != null )
210219 {
@@ -215,10 +224,12 @@ private static void StashPopObjects(RuntimeDataStorage storage)
215224 object obj = item . Instance ;
216225 foreach ( var handle in item . Handles )
217226 {
218- CLRObject . Restore ( obj , handle ) ;
227+ var co = CLRObject . Restore ( obj , handle ) ;
228+ storedObjs . Add ( co ) ;
219229 }
220230 }
221231 }
232+ return storedObjs ;
222233 }
223234
224235 private static void StashPushModules ( RuntimeDataStorage storage )
0 commit comments