Add an additional test to RunMemory

Make sure that all of the handles have been freed so that we don't do stupid things.
diff --git a/src/Encrypt.c b/src/Encrypt.c
index 022fcc1..221c870 100644
--- a/src/Encrypt.c
+++ b/src/Encrypt.c
@@ -962,6 +962,7 @@
 
 	pRecip->m_recipientNext = pEncrypt->m_recipientFirst;
 	pEncrypt->m_recipientFirst = pRecip;
+	pRecip->m_encrypt.m_message.m_refCount++;
 
 	pRecipients = _COSE_arrayget_int(&pEncrypt->m_message, INDEX_RECIPIENTS);
 	if (pRecipients == NULL) {
@@ -983,7 +984,6 @@
 							 pRecip->m_encrypt.m_message.m_cbor, &cbor_error),
 		cbor_error);
 
-	pRecip->m_encrypt.m_message.m_refCount++;
 
 	return true;
 
diff --git a/test/encrypt.c b/test/encrypt.c
index a776183..c81aac7 100644
--- a/test/encrypt.c
+++ b/test/encrypt.c
@@ -545,6 +545,7 @@
 		}
 
 		if (!COSE_Enveloped_AddRecipient(hEncObj, hRecip, NULL)) {
+			COSE_Recipient_Free(hRecip);
 			goto returnError;
 		}
 
@@ -573,15 +574,18 @@
 
 			if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
 					Attributes_Countersign_protected)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_Enveloped_add_countersignature(
 					hEncObj, hCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
@@ -606,6 +610,10 @@
 	return f;
 
 returnError:
+	if (hEncObj != NULL) {
+		COSE_Enveloped_Free(hEncObj);
+	}
+	
 	CFails += 1;
 	return 0;
 }
@@ -748,7 +756,7 @@
 	const cn_cbor *pFail;
 	const cn_cbor *pEncrypt;
 	const cn_cbor *pRecipients;
-	HCOSE_ENCRYPT hEnc;
+	HCOSE_ENCRYPT hEnc = NULL;
 	int type;
 	bool fFail = false;
 	bool fFailBody = false;
@@ -922,9 +930,11 @@
 	}
 #endif
 
-	COSE_Encrypt_Free(hEnc);
 
 exitHere:
+	if (hEnc != NULL) {
+		COSE_Encrypt_Free(hEnc);
+	}
 
 	if (fAlgSupport) {
 		if (fFailBody) {
@@ -946,6 +956,10 @@
 	return fAlgSupport ? 1 : 0;
 
 returnError:
+	if (hEnc != NULL) {
+		COSE_Encrypt_Free(hEnc);
+	}
+
 	CFails += 1;
 	return 0;
 }
@@ -1043,15 +1057,18 @@
 
 			if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
 					Attributes_Countersign_protected)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_Encrypt0_add_countersignature(
 					hEncObj, hCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
@@ -1076,6 +1093,10 @@
 	return f;
 
 returnError:
+	if (hEncObj != NULL) {
+		COSE_Encrypt_Free(hEncObj);
+	}
+	
 	CFails += 1;
 	return 1;
 }
diff --git a/test/mac_test.c b/test/mac_test.c
index 2fd1181..1bd82d6 100644
--- a/test/mac_test.c
+++ b/test/mac_test.c
@@ -316,7 +316,8 @@
 int BuildMacMessage(const cn_cbor *pControl)
 {
 	int iRecipient = 0;
-
+	HCOSE_RECIPIENT hRecip = NULL;
+	
 	//
 	//  We don't run this for all control sequences - skip those marked fail.
 	//
@@ -361,8 +362,7 @@
 			goto returnError;
 		}
 
-		HCOSE_RECIPIENT hRecip =
-			COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+		hRecip = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
 		if (hRecip == NULL) {
 			goto returnError;
 		}
@@ -431,6 +431,7 @@
 #endif
 
 		COSE_Recipient_Free(hRecip);
+		hRecip = NULL;
 	}
 
 #if INCLUDE_COUNTERSIGNATURE
@@ -455,14 +456,17 @@
 
 			if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
 					Attributes_Countersign_protected)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_Mac_add_countersignature(hMacObj, hCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
@@ -488,6 +492,12 @@
 	return f;
 
 returnError:
+	if (hMacObj != NULL) {
+		COSE_Mac_Free(hMacObj);
+	}
+	if (hRecip != NULL) {
+		COSE_Recipient_Free(hRecip);
+	}
 	CFails += 1;
 	return 1;
 }
@@ -761,7 +771,6 @@
 	}
 #endif
 
-	COSE_Mac0_Free(hMAC);
 
 	if (fFailBody) {
 		if (!fFail) {
@@ -771,13 +780,21 @@
 			fFail = false;
 		}
 	}
+	
 exitHere:
+	if (hMAC != NULL) {
+		COSE_Mac0_Free(hMAC);
+	}
+	
 	if (fFail) {
 		CFails += 1;
 	}
 	return fUnsuportedAlg ? 0 : 1;
 
 errorReturn:
+	if (hMAC != NULL) {
+		COSE_Mac0_Free(hMAC);
+	}
 	CFails += 1;
 	return (fFail || fUnsuportedAlg) ? 0 : 1;
 }
@@ -859,14 +876,17 @@
 
 			if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
 					Attributes_Countersign_protected)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_Mac0_add_countersignature(hMacObj, hCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
@@ -892,6 +912,7 @@
 	return f;
 
 returnError:
+	COSE_Mac0_Free(hMacObj);
 	CFails += 1;
 	return 1;
 }
diff --git a/test/sign.c b/test/sign.c
index 870adc1..f6ac539 100644
--- a/test/sign.c
+++ b/test/sign.c
@@ -31,12 +31,14 @@
 	const cn_cbor *pFail;
 	const cn_cbor *pSign;
 	const cn_cbor *pSigners;
-	HCOSE_SIGN hSig;
+	HCOSE_SIGN hSig = NULL;
+	HCOSE_SIGNER hSigner = NULL;
 	int type;
 	int iSigner;
 	bool fFail = false;
 	bool fFailBody = false;
 	bool fNoSupportAlg = false;
+	HCOSE_COUNTERSIGN h = NULL;
 
 	pFail = cn_cbor_mapget_string(pControl, "fail");
 	if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) {
@@ -76,14 +78,12 @@
 
 		cn_cbor *pkey = BuildKey(cn_cbor_mapget_string(pSigners, "key"), false);
 		if (pkey == NULL) {
-			fFail = true;
-			continue;
+			goto returnError;
 		}
 
 		HCOSE_SIGNER hSigner = COSE_Sign_GetSigner(hSig, iSigner, NULL);
 		if (hSigner == NULL) {
-			fFail = true;
-			continue;
+			goto returnError;
 		}
 		if (!SetReceivingAttributes(
 				(HCOSE)hSigner, pSigners, Attributes_Signer_protected)) {
@@ -91,8 +91,7 @@
 		}
 
 		if (!COSE_Signer_SetKey(hSigner, pkey, NULL)) {
-			fFail = true;
-			continue;
+			goto returnError;
 		}
 
 		cn_cbor *alg = COSE_Signer_map_get_int(
@@ -142,8 +141,7 @@
 			for (int counterNo = 0; counterNo < count; counterNo++) {
 				bool noSignAlg = false;
 
-				HCOSE_COUNTERSIGN h =
-					COSE_Signer_get_countersignature(hSigner, counterNo, 0);
+				h =	COSE_Signer_get_countersignature(hSigner, counterNo, 0);
 				if (h == NULL) {
 					fFail = true;
 					continue;
@@ -288,6 +286,13 @@
 	return fNoSupportAlg ? 0 : 1;
 
 returnError:
+	if (hSigner != NULL) {
+		COSE_Signer_Free(hSigner);
+	}
+	if (hSig != NULL) {
+		COSE_Sign_Free(hSig);
+	}
+	
 	CFails += 1;
 	return 0;
 }
@@ -303,7 +308,8 @@
 int BuildSignedMessage(const cn_cbor *pControl)
 {
 	int iSigner;
-
+	HCOSE_SIGNER hSigner = NULL;
+	
 	//
 	//  We don't run this for all control sequences - skip those marked fail.
 	//
@@ -347,7 +353,7 @@
 			goto returnError;
 		}
 
-		HCOSE_SIGNER hSigner = COSE_Signer_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
+		hSigner = COSE_Signer_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
 		if (hSigner == NULL) {
 			goto returnError;
 		}
@@ -387,16 +393,19 @@
 
 				if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
 						Attributes_Countersign_protected)) {
+					COSE_CounterSign_Free(hCountersign);
 					goto returnError;
 				}
 
 				if (!COSE_CounterSign_SetKey(
 						hCountersign, pkeyCountersign, NULL)) {
+					COSE_CounterSign_Free(hCountersign);
 					goto returnError;
 				}
 
 				if (!COSE_Signer_add_countersignature(
 						hSigner, hCountersign, NULL)) {
+					COSE_CounterSign_Free(hCountersign);
 					goto returnError;
 				}
 
@@ -428,14 +437,17 @@
 
 			if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
 					Attributes_Countersign_protected)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_Sign_add_countersignature(hSignObj, hCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
@@ -461,6 +473,13 @@
 	return f;
 
 returnError:
+	if (hSignObj != NULL) {
+		COSE_Sign_Free(hSignObj);
+	}
+	if (hSigner != NULL) {
+		COSE_Signer_Free(hSigner);
+	}
+	
 	CFails += 1;
 	return 1;
 }
@@ -710,7 +729,6 @@
 	}
 #endif
 
-	COSE_Sign1_Free(hSig);
 
 	if (fFailBody) {
 		if (!fFail) {
@@ -722,6 +740,9 @@
 	}
 
 exitHere:
+	if (hSig != NULL) {
+		COSE_Sign1_Free(hSig);
+	}
 
 	if (fFail) {
 		CFails += 1;
@@ -729,6 +750,10 @@
 	return fNoAlgSupport ? 0 : 1;
 
 returnError:
+	if (hSig != NULL) {
+		COSE_Sign1_Free(hSig);
+	}
+
 	CFails += 1;
 	return 0;
 }
@@ -801,15 +826,18 @@
 
 			if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
 					Attributes_Countersign_protected)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
 			if (!COSE_Sign1_add_countersignature(
 					hSignObj, hCountersign, NULL)) {
+				COSE_CounterSign_Free(hCountersign);
 				goto returnError;
 			}
 
@@ -835,6 +863,9 @@
 	return f;
 
 returnError:
+	if (hSignObj != NULL) {
+		COSE_Sign1_Free(hSignObj);
+	}
 	CFails += 1;
 	return 1;
 }
diff --git a/test/test.c b/test/test.c
index 1af4a38..6a794ef 100644
--- a/test/test.c
+++ b/test/test.c
@@ -953,6 +953,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 
@@ -966,6 +972,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 #else
@@ -985,6 +997,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 
@@ -998,6 +1016,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 #else
@@ -1017,6 +1041,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 
@@ -1030,6 +1060,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 #else
@@ -1049,6 +1085,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 
@@ -1062,6 +1104,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 #else
@@ -1081,6 +1129,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 
@@ -1094,6 +1148,12 @@
 				if (IsContextEmpty(context) != 0) {
 					CFails += 1;
 				}
+#ifndef NDEBUG
+				if (!AreListsEmpty()) {
+					CFails += 1;
+					break;
+				}
+#endif
 				FreeContext(context);
 			}
 #else
@@ -1101,8 +1161,8 @@
 			fBuildDone = true;
 #endif
 		}
+		CFails = 0;
 	}
-	CFails = 0;
 	context = NULL;
 #else
 	return;