Index: channels/chan_dahdi.c
===================================================================
--- channels/chan_dahdi.c	(revision 134653)
+++ channels/chan_dahdi.c	(working copy)
@@ -7429,10 +7429,18 @@
 {
 	struct mwi_thread_data *mtd = data;
 	struct timeval timeout_basis, pause, now;
+	unsigned char *cidspill;
+	int cidpos = 0, cidlen = 0;
 	int x, i, res;
 	int num_read;
 	enum mwisend_states mwi_send_state = MWI_SEND_SPILL; /*Assume FSK only */
 
+	cidspill = ast_calloc(1, MAX_CALLERID_SIZE);
+	if (!cidspill) {
+		mtd->pvt->mwisendactive = 0;
+		goto quit;
+	}
+
 	/* Determine how this spill is to be sent */
 	if(mwisend_rpas) {
 		mwi_send_state = MWI_SEND_SA;
@@ -7440,19 +7448,13 @@
 
 	gettimeofday(&timeout_basis, NULL);
 	
-	mtd->pvt->cidspill = ast_calloc(1, MAX_CALLERID_SIZE);
-	if (!mtd->pvt->cidspill) {
-		mtd->pvt->mwisendactive = 0;
-		ast_free(mtd);
-		return NULL;
-	}
 	x = DAHDI_FLUSH_BOTH;
 	res = ioctl(mtd->pvt->subs[SUB_REAL].zfd, DAHDI_FLUSH, &x);
 	x = 3000;
 	ioctl(mtd->pvt->subs[SUB_REAL].zfd, DAHDI_ONHOOKTRANSFER, &x);
-	mtd->pvt->cidlen = vmwi_generate(mtd->pvt->cidspill, has_voicemail(mtd->pvt), CID_MWI_TYPE_MDMF_FULL,
+	cidlen = vmwi_generate(cidspill, has_voicemail(mtd->pvt), CID_MWI_TYPE_MDMF_FULL,
 									 AST_LAW(mtd->pvt), mtd->pvt->cid_name, mtd->pvt->cid_num, 0);
-	mtd->pvt->cidpos = 0;
+	cidpos = 0;
 
 	while (MWI_SEND_DONE != mwi_send_state) {
 		num_read = 0;
@@ -7527,16 +7529,16 @@
 			case MWI_SEND_SPILL:
 				/* We read some number of bytes.  Write an equal amount of data */
 				if(0 < num_read) {
-					if (num_read > mtd->pvt->cidlen - mtd->pvt->cidpos)
-						num_read = mtd->pvt->cidlen - mtd->pvt->cidpos;
-					res = write(mtd->pvt->subs[SUB_REAL].zfd, mtd->pvt->cidspill + mtd->pvt->cidpos, num_read);
+					if (num_read > cidlen - cidpos)
+						num_read = cidlen - cidpos;
+					res = write(mtd->pvt->subs[SUB_REAL].zfd, cidspill + cidpos, num_read);
 					if (res > 0) {
-						mtd->pvt->cidpos += res;
-						if (mtd->pvt->cidpos >= mtd->pvt->cidlen) {
-							ast_free(mtd->pvt->cidspill);
-							mtd->pvt->cidspill = NULL;
-							mtd->pvt->cidpos = 0;
-							mtd->pvt->cidlen = 0;
+						cidpos += res;
+						if (cidpos >= cidlen) {
+							ast_free(cidspill);
+							cidspill = NULL;
+							cidpos = 0;
+							cidlen = 0;
 							mwi_send_state = MWI_SEND_CLEANUP;
 						}
 					} else {
@@ -7556,14 +7558,14 @@
 		}
 	}
 quit:
-		if(mtd->pvt->cidspill) {
-	ast_free(mtd->pvt->cidspill);
-	mtd->pvt->cidspill = NULL;
-		}
-		mtd->pvt->mwisendactive = 0;
-		ast_free(mtd);
+	if(cidspill) {
+		ast_free(cidspill);
+		cidspill = NULL;
+	}
 
-		return NULL;
+	mtd->pvt->mwisendactive = 0;
+	ast_free(mtd);
+	return NULL;
 }
 
 
@@ -7901,7 +7903,7 @@
 					if (last) {
 						if (!last->mwisendactive &&	 last->sig & __DAHDI_SIG_FXO) {
 							res = has_voicemail(last);
-							if (last->msgstate != res) {
+							if (last->msgstate != res) do {
 
 								/* This channel has a new voicemail state,
 								* initiate a thread to send an MWI message
@@ -7911,22 +7913,31 @@
 								struct mwi_thread_data *mtd;
 								res2 = ioctl(last->subs[SUB_REAL].zfd, DAHDI_VMWI, res);
 								if (res2) {
+									/* XXX This seems to be sometimes OK from my tests as it seems like 
+									 * we want to keep trying... but it's VERY QUESTIONABLE */
+									#if 0
 									ast_log(LOG_DEBUG, "Unable to control message waiting led on channel %d: %s\n", last->channel, strerror(errno));
+									#endif
+									found++;
+									break;
 								}
 								pthread_attr_init(&attr);
 								pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 								if ((mtd = ast_calloc(1, sizeof(*mtd)))) {
 									last->msgstate = res;
 									mtd->pvt = last;
+									mtd->pvt->cidspill = ast_calloc(1, MAX_CALLERID_SIZE);
 									last->mwisendactive = 1;
-									if (ast_pthread_create_background(&threadid, &attr, mwi_send_thread, mtd)) {
+									if (!mtd->pvt->cidspill || ast_pthread_create_background(&threadid, &attr, mwi_send_thread, mtd)) {
 										ast_log(LOG_WARNING, "Unable to start mwi send thread on channel %d\n", last->channel);
+										if (mtd->pvt->cidspill)
+											ast_free(mtd->pvt->cidspill);
 										ast_free(mtd);
 										last->mwisendactive = 0;
 									}
 								}
 								found ++;
-							}
+							} while(0);
 						}
 						last = last->next;
 					}
