51448

Segmentation fault when calling class from matlab

Question:

I am getting a segmentation fault with the following code, and I really dont know what is happening here. It seems that it has something to do with the pointer in the Master-class, but I am not sure how to solve this. I have the following code:

class Shape { public: Shape(){} ~Shape(){} virtual void draw() = 0; }; class Circle : public Shape { public: Circle(){} ~Circle(){} void draw() { printf("circle"); // code for drawing circle } }; class Line : public Shape { public: Line() {} ~Line() {} void draw() { printf("line"); // code for drawing line } }; class Master { public: Shape* member_shape; public: Master() {} ~Master() {} void add_shape_circle() { member_shape = new Circle(); } void add_shape_line() { member_shape = new Line(); } }; Master* master_object;

Do you have any clue how to get this code working? Thanks.

EDIT (added main function):

Actually there exists no main-function like the following in my code, because I am using the code in a MATLAB c-mex function. But it should look like this:

//... classes from above here int main() { master_object = new Master(); master_object->add_shape_circle(); master_object->member_shape->draw(); // segmentation error here return 0; } <ol start="2"><li>EDIT</li> </ol>

The error does not occur if I instatiate the Circle object dircetly in the Master-constructor. But then there is no way to choose between Line and Circle. Example: If I change my Master class to the following, then the function call master_object->member_shape->draw() does not lead to an error.

class Master { public: Shape* member_shape; public: Master() { member_shape = new Circle(); } ~Master() {} void add_shape_circle(){} void add_shape_line(){} };

So, there is something up with this uninitialized pointer...I think.

Answer1:

The issue might be related to your using printf. From <a href="https://www.mathworks.com/help/matlab/matlab_external/creating-c-mex-files.html?requestedDomain=www.mathworks.com" rel="nofollow">https://www.mathworks.com/help/matlab/matlab_external/creating-c-mex-files.html?requestedDomain=www.mathworks.com</a> :

<blockquote>

Using cout or the C-language printf function does not work as expected in C++ MEX files. Use the mexPrintf function instead.

</blockquote>

Recommend

  • comparing two text file and find out the related word in python
  • List as a function argument - modifications discarded
  • Performance slowdown when using Julia's built-in functions
  • Parse text file and store fields in hash table in Perl
  • Google script to remove duplicate rows in spreadsheet and keep the most recent entry based on timest
  • lazy load plugin to load images as user scrolls?
  • how to request user permissions using facebook api in js?
  • Python — confused by numpy's piecewise function
  • Return an average of last or first two rows from a different group (indicated by a variable)
  • error unknown avd
  • Strip down any parameter from URL which is not whitelisted
  • Determine windows system wide font
  • hexagonal lattice which is randomly colored with black and white color
  • Count occurrences of strings in Java
  • How can I limit pygame.draw.circle?
  • Using glGetFloatv to retrieve the modelview matrix in pyglet
  • How do js animations work?
  • Plotly python how to draw unbounded lines and spans?
  • Trying to stream 2 way audio over TCP?
  • Encoding in networkx
  • What does this mean? (Google Maps API)
  • Antialias on clipPath on layout
  • fabric.js straight line and select on click
  • FOSUserBundle force user to write a different password
  • C++/CLI Thread synchronization including managed and unmanaged code
  • How to save dynamically created textboxes and their values
  • Using HTML/CSS for UI in XNA?
  • c# winform DrawToBitmap offscreen
  • Casting between Interfaces and Classes
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Zoom in and out of jPanel
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • What and where is mdimport
  • Debug.DrawLine not showing in the GameView
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Google cloud sdk not working when python points python3