@@ -282,7 +282,48 @@ public override bool VisitClassDecl(Class @class)
282282 if ( returnType . IsAddress ( ) )
283283 Context . Return . Write ( HandleReturnedPointer ( @class , qualifiedClass ) ) ;
284284 else
285- Context . Return . Write ( $ "{ qualifiedClass } .{ Helpers . CreateInstanceIdentifier } ({ Context . ReturnVarName } )") ;
285+ {
286+ if ( Context . MarshalKind == MarshalKind . NativeField ||
287+ Context . MarshalKind == MarshalKind . Variable ||
288+ ! originalClass . HasNonTrivialDestructor )
289+ {
290+ Context . Return . Write ( $ "{ qualifiedClass } .{ Helpers . CreateInstanceIdentifier } ({ Context . ReturnVarName } )") ;
291+ }
292+ else
293+ {
294+ Context . Before . WriteLine ( $@ "var __{ Context . ReturnVarName } = {
295+ qualifiedClass } .{ Helpers . CreateInstanceIdentifier } ({ Context . ReturnVarName } );" ) ;
296+ Method dtor = originalClass . Destructors . First ( ) ;
297+ if ( dtor . IsVirtual )
298+ {
299+ var i = VTables . GetVTableIndex ( dtor ) ;
300+ int vtableIndex = 0 ;
301+ if ( Context . Context . ParserOptions . IsMicrosoftAbi )
302+ vtableIndex = @class . Layout . VFTables . IndexOf ( @class . Layout . VFTables . First (
303+ v => v . Layout . Components . Any ( c => c . Method == dtor ) ) ) ;
304+ Context . Before . WriteLine ( $@ "var __vtables = new IntPtr[] {{ {
305+ string . Join ( ", " , originalClass . Layout . VTablePointers . Select (
306+ x => $ " * (IntPtr*) ({ Helpers . InstanceIdentifier } + { x . Offset } )") ) } }};" ) ;
307+ Context . Before . WriteLine ( $ "var __slot = *(IntPtr*) (__vtables[{ vtableIndex } ] + { i } * sizeof(IntPtr));") ;
308+ Context . Before . Write ( $ "Marshal.GetDelegateForFunctionPointer<{ dtor . FunctionType } >(__slot)({ Helpers . InstanceIdentifier } ") ;
309+ if ( dtor . GatherInternalParams ( Context . Context . ParserOptions . IsItaniumLikeAbi ) . Count > 1 )
310+ {
311+ Context . Before . WriteLine ( ", 0" ) ;
312+ }
313+ Context . Before . WriteLine ( ");" ) ;
314+ }
315+ else
316+ {
317+ string suffix = string . Empty ;
318+ var specialization = @class as ClassTemplateSpecialization ;
319+ if ( specialization != null )
320+ suffix = Helpers . GetSuffixFor ( specialization ) ;
321+ Context . Before . WriteLine ( $@ "{ typePrinter . PrintNative ( originalClass ) } .dtor{
322+ suffix } (new { typePrinter . IntPtrType } (&{ Context . ReturnVarName } ));" ) ;
323+ }
324+ Context . Return . Write ( $ "__{ Context . ReturnVarName } ") ;
325+ }
326+ }
286327
287328 return true ;
288329 }
0 commit comments