SDK vector problem. Keeps crashing

Apr 22, 2010 at 6:49pm

SDK vector problem. Keeps crashing

Hi

I’ve been fiddling around with creating my own max/msp objects in C++. I’m trying to figure out how i can manipulate lists by using the vector class. What my code is suppose to do is to take a vector as an input, change every other value in the vector to zero and then output the vector again. My object keeps crashing and I can’t figure out why. Is there anybody out there with some experience in this? Any help would be great thanks!
My code looks like this:

#include “ext.h”
#include “ext_obex.h”
#include “math.h”
#include “stdio.h”
#include “ext_strings.h”
#include “ext_common.h”
#include “ext_systhread.h”
#include
#include

using namespace std;

typedef struct _list_remove {
t_object p_ob;
vector my_vector;
void *list_outlet;
} list_remove;

// these are prototypes for the methods that are defined below
void list_remove_bang(list_remove *x);
void list_remove_cal(list_remove *x, vector vector_in);
void list_remove_assist(list_remove *x, void *b, long m, long a, char *s);
void *list_remove_new(long n);

t_class *list_remove_class;
t_symbol *list_remove_symbol;

//————————————————————————–

int main(void) {
t_class *c;

c = class_new(“list_remove”, (method) list_remove_new, (method)NULL, sizeof(list_remove), 0L, A_DEFLONG, 0);

class_addmethod(c, (method)list_remove_bang, “bang”, 0);
class_addmethod(c, (method)list_remove_cal, “list”, A_GIMME, 0);
class_addmethod(c, (method)list_remove_assist, “assist”, A_CANT, 0);

class_register(CLASS_BOX, c);
list_remove_class = c;

post(“list_remove object loaded…”,0);
return 0;
}

//————————————————————————–

void *list_remove_new(long n)
{
list_remove *x;

x = (list_remove *)object_alloc(list_remove_class);

inlet_new (x, NULL);

x->list_outlet = listout(x);

post(“list_remove object added”,0);

return(x);
}

void list_remove_assist(list_remove *x, void *b, long m, long a, char *s) {
if (m == ASSIST_OUTLET)
sprintf(s,”list Out”);
else {
switch (a) {
case 0:
sprintf(s,”Inlet %ld: Bang”, a);
break;

case 1:
sprintf(s,”Inlet %ld: list In”, a);
break;
}
}
}

void list_remove_bang(list_remove *x) {
t_atom argv[x->my_vector.size()];
outlet_list(x->list_outlet,NULL,x->my_vector.size(),argv);
}

void list_remove_cal(list_remove *x, vector vector_in) {
x->my_vector = vector_in;
int value = 0;
for(unsigned int z=1; zmy_vector.size(); z+=2){
x->my_vector.at(z) = value;
}
list_remove_bang(x);
}

#49955
Apr 22, 2010 at 7:39pm

Here’s a little update :) I found out that you can’t use vectors as inputs for the methods, but instead need to use the atom class, so my functions now looks like this:

void list_remove_bang(list_remove *x) {
}

void list_remove_cal(list_remove *x, t_symbol *msg, long argc, t_atom *argv) {
for(int i=0; i
double value = atom_getfloat(argv+1);
x->my_vector[i].push_back(value);
}
double value2 = 0;
for(int z=1; z
x->my_vector[z].push_back(value2);
}
outlet_list(x->list_outlet,NULL,argc,argv);
}

But it’s still the same problem. Max crashes as soon as i connect the object to a list

#179392
Apr 22, 2010 at 9:57pm

After searching through the documentation I found the solution. Apparently the Atom class is the only solution to use here, cuz that is what the inlets and the outlet functions takes as inputs. I stopped using the vector class and implemented with only the Atom class, and it worked. It may only be me that is stupid enough to have spend so much time fiddleling with this :) but anyway, here is the working code…

#include “ext.h”
#include
#include “ext_obex.h”
#include “math.h”
#include “stdio.h”
#include “ext_strings.h”
#include “ext_common.h”
#include “ext_systhread.h”
#include
#include

using namespace std;

typedef vector a_vector;

typedef struct _list_remove {
t_object p_ob;
Atom my_vector[700];
int my_vector_size;
void *list_outlet;
} list_remove;

void list_remove_bang(list_remove *x);
void list_remove_cal(list_remove *x, t_symbol *msg, long argc, Atom *argv) ;
void list_remove_assist(list_remove *x, void *b, long m, long a, char *s);
void *list_remove_new();

t_class *list_remove_class;
t_symbol *list_remove_symbol;

//————————————————————————–

int main(void) {
t_class *c;

c = class_new(“list_remove”, (method) list_remove_new, (method)NULL, sizeof(list_remove), 0L, A_DEFLONG, 0);

class_addmethod(c, (method)list_remove_bang, “bang”, 0);
class_addmethod(c, (method)list_remove_cal, “list”, A_GIMME, 0);
class_addmethod(c, (method)list_remove_assist, “assist”, A_CANT, 0);

class_register(CLASS_BOX, c);
list_remove_class = c;

post(“list_remove object loaded…”,0);
return 0;
}

//————————————————————————–

void *list_remove_new()
{
list_remove *x;

x = (list_remove *)object_alloc(list_remove_class);

inlet_new (x, NULL);

x->list_outlet = listout(x);

post(“list_remove object added”,0);

return(x);
}

void list_remove_assist(list_remove *x, void *b, long m, long a, char *s) {
if (m == ASSIST_OUTLET)
sprintf(s,”list Out”);
else {
switch (a) {
case 0:
sprintf(s,”Inlet %ld: Bang”, a);
break;
case 1:
sprintf(s,”Inlet %ld: list In”, a);
break;
}
}
}

void list_remove_bang(list_remove *x) {
}

void list_remove_cal(list_remove *x, Symbol *msg, long argc, Atom *argv) {

x->my_vector_size = argc;

for(int i=0; i
switch(argv[i].a_type) {
case A_LONG: SETLONG(x->my_vector+i, argv[i].a_w.w_long);
break;
case A_FLOAT: SETFLOAT(x->my_vector+i, argv[i].a_w.w_float);
break;
case A_SYM: SETSYM(x->my_vector+i, argv[i].a_w.w_sym);
break;
}
}
float value2 = 0;
for(int z=1; z
SETFLOAT(x->my_vector+z, value2);
}
outlet_list(x->list_outlet,0L,x->my_vector_size,x->my_vector);
}

#179393
Apr 29, 2010 at 1:55am

Hi,

FWIW, I believe the problem with your code was this:

typedef struct _list_remove {
    t_object p_ob;
    vector  my_vector;
    void *list_outlet;
} list_remove;

You can declare a std::vector like this in your struct, because the vector’s constructor is never called. Instead you should declare it as a pointer to a vector, and then use the new/delete operators to alloc and free it in your new and free methods.

Cheers

#179394

You must be logged in to reply to this topic.