C++ string

Apr 19, 2010 at 1:35pm

C++ string

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.

Cheers

#49860
Apr 19, 2010 at 6:56pm

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.

#include

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

and remember to deallocate in the myobj_free:

x->~MyObj();

3) Use the templates I made for C++ externals: http://www.mat.ucsb.edu/~wakefield/software.html#maxcpp
4) Forget about C++

#179061
Apr 19, 2010 at 11:01pm

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.

Thank’s

#179062

You must be logged in to reply to this topic.