Create and insert material
-
On 02/11/2016 at 13:06, xxxxxxxx wrote:
My main confusion is with freeing memory.
I thought the main advantage if AutoAlloc is, that you need not to worry about freeing memory.
I know now - reading all the posts above - that you only need to worry about freeing memory if you are the owner.
If cinema is the owner, for example after an insert, you need not to worry about freeing memory.
So, if you are going to insert, no need for autoalloc.
But always check whether the allocation was successful!@Andreas: can you give some example when to free memory?
Another thing is indeed the example Scott mentioned.
BaseMaterial *mat = BaseMaterial::Alloc(Mmaterial); if(!mat) return false; //Is not the same thing as this BaseMaterial *mat = nullptr; if(condition) { mat = BaseMaterial::Alloc(Mmaterial); if(!mat) return false; }
What is the difference and what is the best way?
Again, I need to think in detail about all the information I got.
In the mean time, I have some good code snippets I can use now.@Scott, thanks for joining in the discussion and giving all the information and asking the good questions.
@Andreas, thanks for all the information and patience. -
On 04/11/2016 at 10:15, xxxxxxxx wrote:
Hi Pim
Originally posted by xxxxxxxx
I thought the main advantage if AutoAlloc is, that you need not to worry about freeing memory.
I know now - reading all the posts above - that you only need to worry about freeing memory if you are the owner.
If cinema is the owner, for example after an insert, you need not to worry about freeing memory.
So, if you are going to insert, no need for autoalloc.Right, I'd just not put the last sentence so black and white. Check the goodUseOfAutoAlloc() example in my last point. Although the material gets inserted later on, it still makes sense to use AutoAlloc. It's more about how many places you have (e.g. error exits), where you'd need to pay attention to free the material before inserting it into the document.
Originally posted by xxxxxxxx
But always check whether the allocation was successful!
Yes, please
Originally posted by xxxxxxxx
@Andreas: can you give some example when to free memory?
Hm? Actually I think by now we have several such examples in this thread.
If you allocate memory, you are responsible to free it, except when transferring the ownership to somebody else (like C4D).So, let's ignore AutoAlloc for a moment, maybe it adds too much to your confusion.
Bool AddMyGreatMaterial(BaseDocument* doc) { BaseMaterial *mat = BaseMaterial::Alloc(Mmaterial); if(!mat) return false; // ...adding shaders to the material... // in the process an error occurs if (error when adding shader) { // Here the material needs to be freed, // otherwise the material is lost (you are owner, // but you'll loose the pointer after returning from the function), // in consequence you'd have a memory leak BaseMaterial::Free(mat); return false; } // ... imagine the function to me more complex. More allocations, more error exits... // ... and finally the success case: doc->InsertMaterial(mat); return true;
Of course there are a plethora of other cases, where you might need to free memory. For example on functions, where the docs say, that the caller owns the returned object. Unless you pass the ownership on to somebody else, you'd be responsible.
It all comes down to ownership.
If you are the owner and don't need the memory anymore, you have to free it.
If you are the owner and are about to loose the reference to that memory (pointer), you either need to free it or should think of another place to store the pointer.Maybe these links on Memory Leaks help to shed some more light. Just my first finds, I'm sure by a little more searching you can find way better ones: stackoverflow: "What is a memory leak", Memory Leak in Wikipedia and Microsoft's contribution to the topic.
Originally posted by xxxxxxxx
// Version A BaseMaterial *mat = BaseMaterial::Alloc(Mmaterial); if(!mat) return false; //Is not the same thing as this // Version B BaseMaterial *mat = nullptr; if(condition) { mat = BaseMaterial::Alloc(Mmaterial); if(!mat) return false; } // HERE: mat needs to be checked for nullptr again!
What is the difference and what is the best way?
I'm not sure I can answer this question. After all this example is so stripped down, that I don't see a best way. It depends what you want to achieve.
The difference is in version A, you have a allocated Material pointed to by mat after those two lines of code.
In version B, it depends on the condition, if you have a valid mat pointer after those lines.But I'm really not sure, that's what you were asking for.
In the end code is always also a matter of taste, just like spoken language. So the best way might depend on the eye of the coder... with one difference, regardless of what the best way looks like, it has to be correct. And that's also true for version B as long as you add another nullptr check after the scope of the if.
I know, memory allocations and memory leaks can be a tough topic. So I suggest, you take some time to digest this thread and maybe also take the chance to read some more on these topics. Afterwards the entire AutoAlloc topic will be a breeze and you'll easily see the benefits.
-
On 04/11/2016 at 10:32, xxxxxxxx wrote:
Thanks Andreas.
I will indeed take some time to digest this thread and read some more on these topics.
Thanks for all the support.Regards, Pim