Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Visual-Studio BSTR
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 at 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 at 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 at 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 1

Have you tried free() instead of delete?
  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  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 458
1 OriginalGriff 344
2 George Jonsson 233
3 Animesh Datta 130
4 Shemeemsha RA 128
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 26 Oct 2012
Copyright © CodeProject, 1999-2014
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