Forums > Dev

C++ string

Apr 19 2010 | 1:35 pm

Dear Max5 externals C++ programmers,

I have a C++ class with a string as (protected) member. I have to set the string and output it. That works perfectly when I stay in C++ and don’t try to include this in a Max5 external.
However, when I try to encapsulate my C++ code in the C Max API, any operation on the string makes Max5 crash.
When I change my C++ string member for a char* member (C-like), then, everything works.
I can’t figure out why this is happening ?
Is there some confusion between the library provided for C and the C++ class ?
Is it especially forbidden (or unsupported) to use C++ string with the Max5 API ?
I am doing it wrong ?

If you think of any pointers to help me understand what’s going on, I would appreciate…
For now, I will stick with a char* member to have something working. But I would prefer to make it cleaner and use a string when in C++ and a char* when in C.


Apr 19 2010 | 6:56 pm

If there’s an issue using a std::string embedded in your max object, it is probably because the constructor is not getting called.

You have several options:
1) use a pointer, and call new string().
2) use placement new to call your constructor, e.g.


myobj = (MyObj *)object_alloc(myobj_class);
new (myobj) MyObj;

and remember to deallocate in the myobj_free:


3) Use the templates I made for C++ externals:
4) Forget about C++

Apr 19 2010 | 11:01 pm

Hi !

Thank’s for your answer !
I think you’re probably right about the cause of the problem. And I’ll will try the option 1) you suggested.
The other options won’t work for me however because my externals are actually in C. Only the data structure (and the manipulating of it) is in C++. The Max API remains in C and only serve as a framework to use the data structure through Max.

I’ll keep you posted of the results anyway.


Viewing 3 posts - 1 through 3 (of 3 total)

Forums > Dev