From help-request at octave dot org Fri Jul 22 11:42:39 2005 Subject: Re: Octave/C++ From: "John W. Eaton" To: mjm80 at cam dot ac dot uk Cc: help at octave dot org Date: Fri, 22 Jul 2005 12:40:43 -0400 On 22-Jul-2005, Mike Morley wrote: | It occured to me that I needed to allocate some memory to my robot, | otherwise it would cease to be when the C++ subroutine exited. I now have | the code: | | #include | #include | #include You probably don't need to include dynamic-ld.h. | #include | | using namespace std; I would recommend avoiding this and instead prefixing all the names you actually use from the std namespace with "std::". | extern "C" { | #include "robot_instr.h" | #include "robot_link.h" | } This works, but it would be better if the robot_instr.h and robot_link.h files could be mad C++ friendly. | class Bot: public octave_base_value | { | public: | //constructor | Bot(void): octave_base_value() | { | robot_link robot; | } This doesn't look right. It simply creates a local variable in the constructor, which will disappear when the constructor exits. Perhaps you want to have a robot_link object as a class data member, and then a constructor like Bot (void) : robot (), octave_base_value () { } (assuming the robot_link object is called robot)? | //destructor | ~Bot(void) | { | delete this; | } It is almost certainly an error to "delete this". Your constructor doesn't use new, so your destructor should not use delete. | DEFUN_DLD(BotMake,,nargouts, | "BotMake creates a new robot to link in to") | { | Bot *b = new Bot(); | return octave_value(b); | } | DEFUN_DLD(BotKill,args,, | "BotKill destroys the Bot") | { | if (args.length() == 1) { | delete ((Bot*)&args(0)); | } | } I dont' think you really want this function. When the octave_value object that holds your Bot object is deleted, it will call the destructor for its Rep object, which will delete the memory. Here is a complete working example (but using a simple typedef for the robot_link object, so that I could avoid having to find the files for that): #include #include typedef int robot_link; class Bot : public octave_base_value { public: Bot (void) : robot (), octave_base_value () { } bool is_defined (void) const { return true; } private: robot_link robot; DECLARE_OCTAVE_ALLOCATOR DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA }; DEFINE_OCTAVE_ALLOCATOR (Bot); DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (Bot, "Bot", "Bot"); DEFUN_DLD (BotMake, , , "BotMake creates a new robot to link in to") { static bool type_loaded = false; if (! type_loaded) { Bot::register_type (); mlock ("BotMake"); // Install Bot ops here... } octave_value retval; return octave_value (new Bot ()); } Compiling this with mkoctfile BotMake.cc and running while true, x = BotMake; end in Octave does not result in a memory leak. You will need a few more functions in your Bot class to make it useful. See the example make_int.cc that is included in the Octave sources. jwe ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------