Intermediate Lesson 10 question
Intermediate Lesson 10 question
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
Re: Intermediate Lesson 10 question
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
Re: Intermediate Lesson 10 question
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
Re: Intermediate Lesson 10 question
Here is the solution
- Attachments
-
- PlatformerPolymorphism Chili.rar
- (94.71 KiB) Downloaded 138 times
What is an opposite of parallel.If you are engineer it is a series.If you are a mathematic-perpendicular
-
- Posts: 432
- Joined: September 26th, 2013, 4:57 pm
- Location: Kentucky
- Contact:
Re: Intermediate Lesson 10 question
Is it just me or should you be using delete[] instead of delete when you use new[]?
Example:
In D3dGraphics constructor, you have
In D3DGraphics destructor, you have
Shouldn't it be this?
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.
Example:
In D3dGraphics constructor, you have
Code: Select all
pSysBuffer = new D3DCOLOR[ SCREENWIDTH * SCREENHEIGHT ];
Code: Select all
delete pSysBuffer;
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.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Intermediate Lesson 10 question
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.
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
Re: Intermediate Lesson 10 question
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
Re: Intermediate Lesson 10 question
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:
T is a templated type determined by args passed and ptr is the ptr passed.
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);
Computer too slow? Consider running a VM on your toaster.
Re: Intermediate Lesson 10 question
Thank you
What is an opposite of parallel.If you are engineer it is a series.If you are a mathematic-perpendicular
Re: Intermediate Lesson 10 question
Yes, if you have a pointer
the code
will attempt to deallocate memory for one Surface object, whereas
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.
Code: Select all
Surface* ptr
Code: Select all
delete ptr
Code: Select all
delete [] ptr
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.
Interesting. I never thought about where the CRT stores that meta information about an allocated block.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.
Chili