Stalkerx777 Posted November 21, 2014 Share Posted November 21, 2014 Hi. I have a problem with tokenizing UT_String. While in if scope, tokenize works fine, but when it goes out of scope, array memory get trashed. I guess it because of UT_String tmp variable which gets deleted. Why is that happening? Btw, tokenize calling harden() inside, so the string is deep copied... UT_ValArray<UT_String> *mimics_parmnames = new UT_ValArray<UT_String>(); mimics_asset = opdir->findOBJNode(args.argp('m')); if (mimics_asset) { UT_String tmp; tmp = args.argp('c'); tmp.tokenize(*mimics_parmnames); if(mimics_parmnames->entries()) bake_mimics = true; cout << "In: " << *mimics_parmnames << endl; // Everything is ok here } cout << "Out: " << *mimics_parmnames << endl; // Memory trashed here Quote Link to comment Share on other sites More sharing options...
edward Posted November 22, 2014 Share Posted November 22, 2014 The default behaviour of UT_String is always do shallow references. So the elements in mimic_parmnames is referencing the tokenized data in tmp. What you want is do: UT_StringArray mimics_parmnames; This is almost the same as UT_Array<UT_DeepString>. Quote Link to comment Share on other sites More sharing options...
Stalkerx777 Posted November 22, 2014 Author Share Posted November 22, 2014 Oh, that how it is.... Yeah, i knew about shallow reference, i just thought i need a deep copy of my tmp string, not an array of deepstrings. Now i get it. When tokenize calls UT_Array<UT_String>::append(), it actually calls copy constructor of UT_String, which is shallow by default, right? That's why we need UT_DeepString (which is the same as UT_String(ALLWAYS_DEEP)) Now, it works, thanks Edward! Quote Link to comment Share on other sites More sharing options...
edward Posted November 23, 2014 Share Posted November 23, 2014 Right. Making "tmp" deep doesn't help you because it will get deleted when it goes out of scope. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.