Jump to content

UT_String question


Recommended Posts

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
Link to comment
Share on other sites

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>.

Link to comment
Share on other sites

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!  :)

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...