Quantcast
Channel: Adobe Community : Popular Discussions - After Effects SDK
Viewing all articles
Browse latest Browse all 73444

Dynamic Memory Allocation For Array of Objects

$
0
0

So I'm reaching my wits-end with this problem, and am hoping someone on here can see what I'm doing wrong.

 

Basically I have a collection class, let's call it Collection. In there I want a dynamic array of my item class, which for the sake of argument we'll call Item. An instance of Collection is then used within Global Data to be used later on.

 

The collection of items is populated by a member function in Collection, from an external text file. There could be any number of items, which is why I want it to be dynamically allocated.

 

In order to make the memory allocation self-sufficient, I'm also storing in Collection a pointer to the SPBasicSuite, a pointer to PF_HandleSuite1 and a PF_Handle for the memory handle.

 

class Item {
private:    int foo;    char bar[32];    Thing things[5]; // Fixed array of Thing class, defined elsewhere
}

class Collection {
private:
    Item *item_array;     // It's this I want to populate    int num_items;    SPBasicSuite *bsuite;    PF_HandleSuite1 *hsP;    PF_Handle memH;
public:    PF_Err AquireHandleSuite(PF_InData *);    void AllocateCollectionMemory();   void LoadFromDefFile(char *path);
}

PF_Err Collection::AquireHandleSuite(PF_InData *in_data) {
    PF_Err  err = PF_Err_NONE;      bsuite = in_data->pica_basicP;       if(bsuite) {        (*bsuite->AcquireSuite)((char*)kPFHandleSuite, kPFHandleSuiteVersion1, (const void**)&hsP);        if(!hsP) {            err = PF_Err_BAD_CALLBACK_PARAM;        }     } else {            err = PF_Err_BAD_CALLBACK_PARAM;     }     return err;
}


void Collection::AllocateCollectionMemory() {
    memH = hsP->host_new_handle(sizeof(Item) * num_items);    void *memP = hsP->host_lock_handle(memH);    // Use placement new to allocate the array    item_array = new(memP) Item[num_items];
}

 

So during Global Setup I'm locking my handle to Global Data:

 

out_data->global_data = suites.HandleSuite1()->host_new_handle(sizeof(GlobalData));
GlobalData *global_dataP = static_cast<GlobalData *>(suites.HandleSuite1()->host_lock_handle(out_data->global_data));

Which all works fine, then I use:

 

ERR(global_dataP->my_collection.AquireHandleSuite(in_data));


to create the pointers to the PICA basic suite and HandleSuite1 (by the way these are used in the destructor to unlock the memory and dispose of the handle).


Then I call:


 

global_dataP->my_collection.LoadFromDefFile(path);

 


to populate the array. First I get num_items and then I call AllocateCollectionMemory() to do the allocation. All seems good. However it's here where things start to go wrong. It seems that the first element in the array of item gets written OK but the second and upward elements don't. There's no crash as it loops through, seemingly populating nothing but I do get strange behaviours later on when I try and access the array, even immediately after I've populated it within the LoadFromDefFile() method (just testing setting and reading an integer value).


The odd thing is, I'm using "placement new" during Smart Render with a different class, declared in the same way (Wotsit *wotsits) to allocate an array and it works fine. It must be something to do with storing the PICA and HandleSuite pointers but how else would you do it? I've also tried:


Collection test_collection;
ERR(test_collection.AquireHandleSuite(in_data));
test_collection.LoadFromDefFile(path);


i.e. not in Global Data to see if that was the problem, but, alas it had exactly the same results.


Thanks!


Viewing all articles
Browse latest Browse all 73444

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>