Forums > Dev

crash on my bad code

May 21, 2007 | 11:44 pm

Hi there,

I’ve written two attempts at max externals, to calculate the cross product and dot product of 3-float-long lists (e.g. vectors). Well, they crash, and I’m sure it’s because a) my C is really rusty and b) this is my first attempt at writing externals. It’s probably due to bad allocation of memory with my structs and pointers, but the code is really very simple, so if someone has a minute to take a look at it and advise me, that’d be great. The end goal is to do a few more 3-vector and quarternion functions and opensource them.

http://lowfrequency.org/stuff/lfo.crossProduct.dmg

http://lowfrequency.org/stuff/lfo.dotProduct.dmg

Cheers
Evan


May 21, 2007 | 11:47 pm

sorry, forgot to include a help file – http://lowfrequency.org/stuff/lfo.crossProduct.help.zip


May 22, 2007 | 2:28 am

FWIW,
If you don’t mind dependencies on Jitter, have a look at
jit.vecmath.h. There’s a world of vec3 and quat functions in there.
Haven’t taken a look at your code, so I don’t have any advice on that,
but the vector structs in vecmath are there for the borrowing if you
want to throw them in a max object and write your own
dot/crossproducts etc.

wes

On 5/21/07, evan.raskob

wrote:
>
> sorry, forgot to include a help file – http://lowfrequency.org/stuff/lfo.crossProduct.help.zip
>


May 22, 2007 | 12:53 pm

Hey, that’s really good to know… Thanks!


May 22, 2007 | 1:54 pm

I think the problem is how I deal with the inlet proxy – I’m not sure I’m using it correctly. Glancing at the code for vexpr (because I want to be able to store a list in the 2nd inlet of my objects, without triggering a calculation) I did this:

in void *dotProduct_new(t_symbol *s, short ac, t_atom *av) :

x->c_proxy[0] = proxy_new(x,1,&x->c_inNumber);

then I free that memory in void dotProduct_free(t_dotProduct *x) :

if (x->c_proxy) {
freeobject(x->c_proxy[0]);
freebytes(x->c_proxy,(short)(sizeof(void *)));

My (revised) question is, do I need to free that proxy object in a free function, and have I done so correctly?

Thanks in advance.
Evan


May 22, 2007 | 2:07 pm

No, this is wrong, since you’re not allocating x->c_proxy, but rather
declaring it as a variable, which is correct. You can just declare it
as "void *c_proxy" in your object struct, since it’s just a pointer.
Then, in new(),

x->c_proxy = proxy_new(x, 1, &x->c_inNumber);

and in free(),

if (x->c_proxy)
freeobject(x->c_proxy);

You don’t allocate memory yourself for your proxy variables.

jb

Am 22.05.2007 um 15:54 schrieb evan.raskob:

> x->c_proxy[0] = proxy_new(x,1,&x->c_inNumber);
>
>
> then I free that memory in void dotProduct_free(t_dotProduct *x) :
>
> if (x->c_proxy) {
> freeobject(x->c_proxy[0]);
> freebytes(x->c_proxy,(short)(sizeof(void *)));
>
> My (revised) question is, do I need to free that proxy object in a
> free function, and have I done so correctly?


May 22, 2007 | 5:18 pm

Thanks Jeremy, that fixed it.


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