@@ -48,6 +48,8 @@ public void AddOrUpdate(string service, ICredential credential)
48
48
49
49
IntPtr existingCredPtr = IntPtr . Zero ;
50
50
IntPtr credBlob = IntPtr . Zero ;
51
+ IntPtr expiryPtr = IntPtr . Zero ;
52
+ IntPtr attributesPtr = IntPtr . Zero ;
51
53
52
54
try
53
55
{
@@ -101,7 +103,7 @@ public void AddOrUpdate(string service, ICredential credential)
101
103
if ( credential . PasswordExpiry != null )
102
104
{
103
105
byte [ ] expiryBytes = BitConverter . GetBytes ( credential . PasswordExpiry . Value . ToUnixTimeSeconds ( ) ) ;
104
- IntPtr expiryPtr = Marshal . AllocHGlobal ( expiryBytes . Length ) ;
106
+ expiryPtr = Marshal . AllocHGlobal ( expiryBytes . Length ) ;
105
107
Marshal . Copy ( expiryBytes , 0 , expiryPtr , expiryBytes . Length ) ;
106
108
107
109
var attribute = new Win32CredentialAttribute ( )
@@ -113,8 +115,9 @@ public void AddOrUpdate(string service, ICredential credential)
113
115
} ;
114
116
115
117
newCred . AttributeCount = 1 ;
116
- newCred . Attributes = Marshal . AllocHGlobal ( Marshal . SizeOf ( attribute ) ) ;
117
- Marshal . StructureToPtr ( attribute , newCred . Attributes , false ) ;
118
+ attributesPtr = Marshal . AllocHGlobal ( Marshal . SizeOf ( attribute ) ) ;
119
+ Marshal . StructureToPtr ( attribute , attributesPtr , false ) ;
120
+ newCred . Attributes = attributesPtr ;
118
121
}
119
122
120
123
int result = Win32Error . GetLastError (
@@ -134,6 +137,17 @@ public void AddOrUpdate(string service, ICredential credential)
134
137
{
135
138
Advapi32 . CredFree ( existingCredPtr ) ;
136
139
}
140
+
141
+ if ( expiryPtr != IntPtr . Zero )
142
+ {
143
+ Marshal . FreeHGlobal ( expiryPtr ) ;
144
+ }
145
+
146
+ if ( attributesPtr != IntPtr . Zero )
147
+ {
148
+ Marshal . DestroyStructure < Win32CredentialAttribute > ( attributesPtr ) ;
149
+ Marshal . FreeHGlobal ( attributesPtr ) ;
150
+ }
137
151
}
138
152
}
139
153
0 commit comments