Intermediate Lesson 10 question

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Nurlan
Posts: 22
Joined: January 4th, 2016, 3:45 am
Location: Saint-Petersburg. Russia

Intermediate Lesson 10 question

Post by Nurlan » May 30th, 2016, 11:01 pm

When I put a _CrtDumpMemoryLeaks() function in ~Game,and tested it, it shows that we have a memoryleaks. Why we have it?. The second question we have a destructor in SurfaceSequence and destructor in Surface ,so what is destoryed in Surface?
What is an opposite of parallel.If you are engineer it is a series.If you are a mathematic-perpendicular

Nurlan
Posts: 22
Joined: January 4th, 2016, 3:45 am
Location: Saint-Petersburg. Russia

Re: Intermediate Lesson 10 question

Post by Nurlan » May 30th, 2016, 11:37 pm

I mean how destructor works in situation with inheritance?
What is an opposite of parallel.If you are engineer it is a series.If you are a mathematic-perpendicular

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Intermediate Lesson 10 question

Post by chili » May 31st, 2016, 1:30 am

It could be memory that has not yet been destructed (destruction does not end at the end of ~Game() !) or it could be an actual leak somewhere. Best bet is to clean your solution and upload it here. I'll take a look at it and answer your question about the destructor too.
Chili

Nurlan
Posts: 22
Joined: January 4th, 2016, 3:45 am
Location: Saint-Petersburg. Russia

Re: Intermediate Lesson 10 question

Post by Nurlan » May 31st, 2016, 2:40 pm

Here is the solution
Attachments
PlatformerPolymorphism Chili.rar
(94.71 KiB) Downloaded 137 times
What is an opposite of parallel.If you are engineer it is a series.If you are a mathematic-perpendicular

Pindrought
Posts: 432
Joined: September 26th, 2013, 4:57 pm
Location: Kentucky
Contact:

Re: Intermediate Lesson 10 question

Post by Pindrought » May 31st, 2016, 3:11 pm

Is it just me or should you be using delete[] instead of delete when you use new[]?


Example:

In D3dGraphics constructor, you have

Code: Select all

pSysBuffer = new D3DCOLOR[ SCREENWIDTH * SCREENHEIGHT ];
In D3DGraphics destructor, you have

Code: Select all

delete pSysBuffer;
Shouldn't it be this?

Code: Select all

delete[] pSysBuffer;

It appears that in the rest of the code you are using delete[] instead of delete so i'm guessing this was just in this specific part that this occurred.
PM me if you need to contact me. Thanks to all the helpful people on this forum especially to Chili.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Intermediate Lesson 10 question

Post by albinopapa » May 31st, 2016, 4:38 pm

Yes, allocations with new/new[] should be deallocated with delete/delete[].

When you call delete, it calls the destructor of the pointer that you pass it. When you call delete[] it calls the destructor for each item in the array that you pass it. That is why it's so important to use the correct new/delete or new[]/delete[] pair.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Nurlan
Posts: 22
Joined: January 4th, 2016, 3:45 am
Location: Saint-Petersburg. Russia

Re: Intermediate Lesson 10 question

Post by Nurlan » May 31st, 2016, 5:07 pm

So you mean that delete doesnt actually delete an object, it just call destructor of the object?
What is an opposite of parallel.If you are engineer it is a series.If you are a mathematic-perpendicular

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Intermediate Lesson 10 question

Post by cameron » May 31st, 2016, 5:38 pm

If you delete a ptr created with new[] then it will try to free the WRONG ptr. Calling new[] actually allocates a extra size_t before the data so it can store the number of objects allocated so it knows to what extent to loop and call the destructor of the objects. When delete[] is called it does something similar to:

Code: Select all

char* basePtr = (char*)ptr - sizeof(size_t);
const size_t size = *basePtr;
for(size_t i  = 0; i < size; i++)
(ptr + i)->~T();

free(basePtr);
T is a templated type determined by args passed and ptr is the ptr passed.
Computer too slow? Consider running a VM on your toaster.

Nurlan
Posts: 22
Joined: January 4th, 2016, 3:45 am
Location: Saint-Petersburg. Russia

Re: Intermediate Lesson 10 question

Post by Nurlan » May 31st, 2016, 6:37 pm

Thank you
What is an opposite of parallel.If you are engineer it is a series.If you are a mathematic-perpendicular

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Intermediate Lesson 10 question

Post by chili » June 1st, 2016, 1:45 am

Yes, if you have a pointer

Code: Select all

Surface* ptr
the code

Code: Select all

delete ptr
will attempt to deallocate memory for one Surface object, whereas

Code: Select all

delete [] ptr
will attempt to deallocate memory for an array of Surface objects.

Now for your questions about the destructors. A Surface object owns a surface, which is basically an array of pixels. A SurfaceSequence object owns an array of surfaces. So the destructor in the SurfaceSequence deallocates the array of Surface objects it owns, which in turn calls the destructor for each Surface object in the array, and for each of these calls to Surface's destructor, Surface's destructor frees the memory for the array of pixels that that Surface object owns.

cameron wrote:Calling new[] actually allocates a extra size_t before the data so it can store the number of objects allocated so it knows to what extent to loop and call the destructor of the objects.
Interesting. I never thought about where the CRT stores that meta information about an allocated block.
Chili

Post Reply