Skip to main content
gamedev.net
🔒 Locked

Problem using vector

Started by stodge 6 replies 450+ views
Original Post
stodge
stodge
I''m using a vector to store and maintain a number of instances of one object.
	msClientConnectionTCP *client;

	typedef vector clientGroup;

		client = acceptSocket->accept();
		if (client != NULL)
		{
			printf("Connection established\n");
			clientConnections.push_back(*client);
 
client is obviously a pointer to a class. so I''m derefencing the pointer when I call push_back. However, for some reason the destructor msClientConnectionTCP::~msClientConnectionTCP is being called by push_back and deleting this instance of the class. Is this normal, or am I doing something fundamentally wrong here? I thought I was on the right track with this. Any help greatly appreciated. Thanks
---------------------http://www.stodge.net
Solo
Solo
How certain are you that it''s not a copy that''s being deleted? Does msClientConnectionTCP require a deep copy?
// Ryan
stodge
stodge
You've lost me; I don't know what a deep copy is!

I don't know that it's not a copy being destroyed, although I think it isn't.

[edited by - stodge on October 4, 2002 12:39:15 AM]
---------------------http://www.stodge.net
Solo
Solo
Example of a class that would require a deep copy:


      
class NeedsDeepCopy
{
public:

NeedsDeepCopy(char *s)
{
text = s
}

~NeedsDeepCopy()
{
delete text;
}

private:
NeedsDeepCopy(){}

char *text;
};




The above class needs a copy constructor defined. Since I've failed to provide one the compiler will use the default copy constructor any time a copy of the object needs to be created. The default copy constructor just copies the class instance byte by byte. That creates two pointers (one in each of the two instances of the class) that point to the same memory location. When the copy is no longer needed the destructor gets called and the text pointer is deleted leaving the original instance of the class with a dangling pointer (a pointer that no longer points to valid data or more likely unallocated memory).

[edited by - Solo on October 5, 2002 1:31:37 AM]

[edited by - Solo on October 5, 2002 1:32:09 AM]
// Ryan
stodge
stodge
Thanks Solo, that did the trick.

I'm having a problem now with a map. I'm adding a single instance into my map, but when I try to erase an entry, it crashes with an access violation.


typedef map[NLsocket, msClientConnectionTCP] clientConnectionGroup;
clientConnectionGroup clientConnections;

msClientConnectionTCP *client;

clientConnections[1] = *client;
clientConnectionGroup::iterator i;
msClientConnectionTCP c;

for (i = clientConnections.begin();
i != clientConnections.end();
i++)
{
clientConnections.erase(1);
}


Do I need to catch this exception. I've been reading some tutorials but none of them do.

Thanks

Edit: replaced angled brackets with square ones as they didn't show due to the HTML replacement.

[edited by - stodge on October 5, 2002 5:11:20 PM]

[edited by - stodge on October 5, 2002 5:12:19 PM]
---------------------http://www.stodge.net
cgoat
cgoat
msClientConnectionTCP *client;
clientConnections[1] = *client;

You're dereferencing an invalid pointer here... (you never created an object or set it to anything, from the code you posted).

Also, doing map.erase() invalidates the iterator, so you can't then do an i++ on it. Plus, why are you doing erase(1)??? There's no point using the iterator if you're going to delete using a key.

[edited by - cgoat on October 5, 2002 5:48:58 PM]
stodge
stodge
An instance of client is a being created elsewhere. I''m not erasing by value (erase(1)), I just did that to save time! Sorry
---------------------http://www.stodge.net
Solo
Solo
If I remember correctly there is an overloaded erase method that takes begining and ending iterators. Try this:

clientConnections.erase(clientConnections.begin(),clientConnections.end());

But also make sure you check the copy constructor and destructor for class you''re using for your key.
// Ryan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.