Click here to Skip to main content
15,892,537 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: Trying to Put HWND Value Into String Pin
redfish344-Jul-06 16:36
redfish344-Jul-06 16:36 
GeneralRe: Trying to Put HWND Value Into String Pin
Jörgen Sigvardsson4-Jul-06 23:04
Jörgen Sigvardsson4-Jul-06 23:04 
GeneralRe: Trying to Put HWND Value Into String [modified] Pin
Eytukan5-Jul-06 4:17
Eytukan5-Jul-06 4:17 
GeneralRe: Trying to Put HWND Value Into String [modified] Pin
Mattias G5-Jul-06 12:58
Mattias G5-Jul-06 12:58 
AnswerRe: Trying to Put HWND Value Into String Pin
Eytukan4-Jul-06 21:36
Eytukan4-Jul-06 21:36 
GeneralRe: Trying to Put HWND Value Into String Pin
Eytukan4-Jul-06 21:38
Eytukan4-Jul-06 21:38 
GeneralRe: Trying to Put HWND Value Into String Pin
Michael Dunn5-Jul-06 7:34
sitebuilderMichael Dunn5-Jul-06 7:34 
QuestionSmart Pointer and STL Container Pin
Greg Yeung2-Jul-06 22:20
Greg Yeung2-Jul-06 22:20 
I have implemented a class that uses a shared reference count content as below:

class SharedContent<br />
{<br />
public:<br />
	SharedContent(unsigned short size) : _size(size), _pCotent(new ContentType[_size]), _count(1) { }<br />
<br />
	unsigned short getSize() const { return _size; }<br />
<br />
	ContentType* getContent() const { return _pContent; }<br />
<br />
	void decCount() const {<br />
		_count--;<br />
		if (_count == 0) {<br />
			// destroy this object<br />
			delete [] _pContent;<br />
			delete this;<br />
		}<br />
	}<br />
<br />
	void incCount() const {<br />
		_count++;<br />
	}<br />
<br />
private:<br />
	unsigned short _size;<br />
	ContentType* _pContent;<br />
	mutable int _count;		///< reference count<br />
};<br />
<br />
class ReferenceObject {<br />
public:<br />
	ReferenceObject() : _content(NULL) { }<br />
<br />
	ReferenceObject(unsigned short size) : _content(new SharedContent(size)) { }<br />
<br />
	ReferenceObject(const ReferenceObject& rhs) : _content(NULL) { *this = rhs; }<br />
<br />
	~ReferenceObject() {<br />
		if (_content != NULL) _content->decCount();<br />
	}<br />
<br />
	ReferenceObject& operator=(const ReferenceObject& rhs) {<br />
		// _content is pointed to old content<br />
		if (_content != NULL) _content->decCount();<br />
		_content = rhs._content;<br />
		// _content is pointed to rhs._content now<br />
		if (_content != NULL) _content->incCount();<br />
	}<br />
<br />
	unsigned short getLength() const {<br />
		return _content == NULL ? 0 : _content->getSize();<br />
	}<br />
<br />
	ContentType* getContent() const {<br />
		return _content == NULL ? NULL : _content->getContent();<br />
	}<br />
<br />
	ReferenceObject clone() const {<br />
		return clone(0, getSize());<br />
	}<br />
<br />
	ReferenceObject clone(int offset, int size) const {<br />
		ByteArray result(length);<br />
		memcpy(result.getContent(), getContent() + offset, size);<br />
		return result;<br />
	}<br />
<br />
	// Some other functions<br />
	// ...<br />
<br />
private:<br />
	SharedContent* _content;<br />
};


When I enqueued the object into a std::deque and dequeued and erased it afterwards, I found that the reference count does not function well such that the counter drops to 0 and raises again. This destroys the SharedContent object and accesses the destroyed content, which causes failure to the program. The code is similar to the following:

void addElement(unsigned short size, std::deque<ReferenceObject>& objectQueue) {<br />
	ReferenceObject obj(size);<br />
	objectQueue.push_back(obj);<br />
}<br />
<br />
int main {<br />
	std::deque<ReferenceObject> objectQueue;<br />
	for (int i = 0; i < 10; ++i) {<br />
		addElement(i, objectQueue);<br />
	}<br />
	std::deque<ReferenceObject>::iterator itr = objectQueue.begin();<br />
	while (itr != objectQueue.begin() + 3) {<br />
		ReferenceObject obj = *itr;<br />
		// do something with obj, push to queue B<br />
		++itr;<br />
	}<br />
	objectQueue.erase(objectQueue.begin(), itr);<br />
	return 0;<br />
}


I use valgrind to check the memory problems, I found that problem occurs at erase() function that the reference count is dropped to 0 when accessing the obj in queue B. Could anyone show me what's wrong with the ReferenceObject? Thank you.

-- modified at 4:20 Monday 3rd July, 2006
AnswerRe: Smart Pointer and STL Container Pin
Stuart Dootson3-Jul-06 2:00
professionalStuart Dootson3-Jul-06 2:00 
GeneralRe: Smart Pointer and STL Container Pin
Greg Yeung3-Jul-06 16:57
Greg Yeung3-Jul-06 16:57 
QuestionWindows application with Infragistics controls [modified] Pin
DeloreanMag30-Jun-06 19:37
DeloreanMag30-Jun-06 19:37 
Questionstd::map issue Pin
f229-Jun-06 20:35
f229-Jun-06 20:35 
AnswerRe: std::map issue [modified] Pin
valikac30-Jun-06 7:03
valikac30-Jun-06 7:03 
GeneralRe: std::map issue (solved) bug in xtree Pin
f230-Jun-06 8:39
f230-Jun-06 8:39 
AnswerRe: std::map issue Pin
Stephen Hewitt1-Jul-06 3:39
Stephen Hewitt1-Jul-06 3:39 
GeneralRe: std::map issue Pin
f21-Jul-06 4:49
f21-Jul-06 4:49 
QuestionA General Window Question Pin
HakunaMatada28-Jun-06 21:25
HakunaMatada28-Jun-06 21:25 
AnswerRe: A General Window Question Pin
Justin Tay28-Jun-06 23:40
Justin Tay28-Jun-06 23:40 
GeneralRe: A General Window Question Pin
HakunaMatada29-Jun-06 0:32
HakunaMatada29-Jun-06 0:32 
QuestionMultithreading Pin
shivditya27-Jun-06 21:51
shivditya27-Jun-06 21:51 
QuestionHow to capture the WM_ONVSCROLL message and then do something in WTL [modified] Pin
wanilyer27-Jun-06 15:02
wanilyer27-Jun-06 15:02 
AnswerRe: How to capture the WM_ONVSCROLL message and then do something in WTL Pin
Justin Tay27-Jun-06 18:18
Justin Tay27-Jun-06 18:18 
GeneralRe: How to capture the WM_ONVSCROLL message and then do something in WTL Pin
wanilyer27-Jun-06 18:49
wanilyer27-Jun-06 18:49 
Questiondata not being inserted/updated Pin
ratish mr27-Jun-06 1:47
ratish mr27-Jun-06 1:47 
AnswerRe: data not being inserted/updated [modified] Pin
Kurt _B27-Jun-06 2:59
Kurt _B27-Jun-06 2:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.