Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm not entirely sure how pointers work in this situation I've created and I'm being careful to avoid memory leaks. I've written the following code;

C++
Packet* packet;

// construct packet from ID and data
switch (packet_id)
{
case 0:
	PacketPlayerMove* p = new PacketPlayerMove();
	// call some methods on p here which aren't present in base class Packet
	packet = p;
}

return packet;

So PacketPlayerMove and many others are implementations of the base class Packet. This method returns a Packet*, so I create a Packet* and assign its value as an instance of any of the inheriting classes, depending on a unique identifier packet_id.

In a nutshell:

packet_id 0: return PacketPlayerMove
packet_id 1: return PacketPlayerChat
etc.

I'm not sure whether I should delete p or not, because I'm not sure if that'll delete packet, as either its reference or value is copied - this is where my knowledge of pointers is hazy.

I will be deleting packet later, but I don't know whether I should delete p now, or if it doesn't need to be deleted.

What I have tried:

-----------------------------------------------
Posted
Updated 9-Jul-18 21:12pm
v6
Comments
Richard MacCutchan 10-Jul-18 3:12am    
If you delete p before returning from this function then packet will no longer be valid. You only have one actual memory block but both p and packet are pointing to it.

You may also have a look at C++ smart pointers, see for instance intro/smart pointers - cppreference.com[^].
 
Share this answer
 
If you are going to delete the pointer later then you should not delete it 'now'. If you have copied it then you will likely have a memory leak. You should be able to test this and detect a memory leak. I use the "#define new DEBUG_NEW" macro all over the place and leaks are detected in debug mode.
 
Share this answer
 
Comments
[no name] 9-Jul-18 12:12pm    
I mean packet will be deleted later but I'm not sure whether to delete p now or not. p is only used to call methods on the implementation rather than the base class.

I'm also getting a ton of errors complaining that DEBUG_NEW is undefined.
Rick York 9-Jul-18 12:42pm    
I use MFC and I believe DEBUG_NEW comes with it. If you are not using MFC then that macro is probably not available to you. There might be some other memory tracking allocator function available though. If you are sure p will be deleted later then don't delete it there unless the object has been copied. Otherwise you will attempt to access invalid data and an exception will occur.
You must understand that a pointer variable is like a normal variable. So you can assign different values to them or the same value to different pointers. But creating an instance with new needs a delete somewhere else.
C++
//some normal int var
int a = 2;
int b = a;

PacketPlayerMove* p = new PacketPlayerMove();//Pointer value NEEDS a delete
PacketPlayerMove* p2 = p;//no new instance only pointer assigment, so both pointers are pointing to the SAME address

delete p;// free the memory.
Learn about pointers in this C++ tutorial. It has some interesting chapters packed with interesting knowledege.

It is best practice to write clear and understandable code, because when working with a lot of pointers a lot of things can go wrong.
 
Share this answer
 
Comments
[no name] 10-Jul-18 4:17am    
So this means I could delete either packet or p, and free the memory.

With PHP, you can delete normal variables using unset(..) to free memory. Would the equivalent here be to set p2 to NULL, then delete p?

Not only would this free the (arguably tiny) amount of resources the pointer itself takes, but it would ensure it can't be used after I intend it to be, reducing the risk of bugs with using the wrong pointer.

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