Click here to Skip to main content
12,397,805 members (55,257 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C Visual-Studio
This is mine structure


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

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

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 25-Oct-12 5:07am
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov 26-Oct-12 11:19am
   
Good point, a 5.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Have you tried free() instead of delete?
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 26 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100