Click here to Skip to main content
15,888,177 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
This is mine structure


C++
typedef struct MESSAGE
{

	 BSTR SerialNo;
	_bstr_t TimeStampIs;
	_bstr_t Status;
	_bstr_t Data;


} MESSAGE, *PMESSAGE;

typedef struct MESSAGENODE
{
	PMESSAGE Message;
	MESSAGENODE* pNext;
} MESSAGENODE, *PMESSAGENODE;



delete function to delete the bstr variable

C++
PMESSAGE pMess;
	while((pMess = GetMachineMessage()) != NULL)
	{
		if(pMess->Data && pMess->SerialNo)
		{
			delete[] pMess->Data;//error here error C2440: 'delete' : cannot convert from '_bstr_t' to 'void *' 
		}
		delete pMess;
	}


This is get machine message

C++
PMESSAGE Message::GetMachineMessage(void)
{
	wLog->WriteDebugLog("Enter GetServerMessage");
	EnterCriticalSection(&MessageQueueCS);
	PMESSAGENODE pRetNode;
	if(MessageQueueFront == NULL)
	{
		pRetNode = NULL;
	} 
	else 
	{
		pRetNode = MessageQueueFront;
		MessageQueueFront = pRetNode->pNext;
		if(MessageQueueFront == NULL)
		{
			MessageQueueBack = NULL;
		} 
		else if(MessageQueueFront->pNext == NULL)
		{
			MessageQueueBack = NULL;
		}
	}

	LeaveCriticalSection(&MessageQueueCS);
	if(pRetNode == NULL) 
	{

		wLog->WriteErrorLog("OUT::GetServerMessage");
		return NULL;
	}
	PMESSAGE pRet = pRetNode->Message;
	delete pRetNode;
	wLog->WriteDebugLog("Exit GetServerMessage");
	return pRet;
}
Posted
Comments
unitrunker 25-Oct-12 11:55am    
Why are you deleting a _bstr_t? The destructor for _bstr_t should call SysFreeString on the underlying BSTR. If you only want to release the Data portion you can do this:

SysFreeString(pMess->Data.Detach());
Sergey Alexandrovich Kryukov 25-Oct-12 16:48pm    
Delete types? Are you sure? :-)
--SA

You cannot (see Sergey answer) and must NOT delete it.
_bstr_t is a C++ class that wraps a BSTR string (hence Data is an instance of a class, that is a object, not a pointer, you cannot delete it).
It automatically calls SysFreeString when appropriate (in your case, possibly, on pMess deletion).
See "_bstr_t Class"[^] at MSDN.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Oct-12 11:19am    
Good point, a 5.
--SA
Here is the exact answer: you cannot delete any types at all, you can only delete instances of types (objects).

(Sorry if it looks too pedantic to you: you need to understand importance of accurate phrasing — this is the real purpose of this post.)
—SA
 
Share this answer
 
Your question has already been answered, but on the other hand SerialNo is a plain BSTR, so make sure you call SysFreeString for that, before you delete the MESSAGE object.
 
Share this answer
 
Have you tried free() instead of delete?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900