allocating new string (re: how to apply oo within max)

Jun 24, 2007 at 9:05pm

allocating new string (re: how to apply oo within max)

Hi johnpitcairn,

Just a short question: in the other thread you advised me to allocate the correct amount of memory for a string copy like this:

str = malloc(strlen(path->s_name));
if(str != NULL) strcpy(str, path->s_name);

…do stuff…

free(str);

instead of char str[1024];

When I use your method I get occasional crashes when reloading the abstraction (oo.call) that contains this external. When I switch back to the old method there are no more crashes.

I wonder, is there something obvious I am overlooking? Apart from adding one more char for “/0″? I suppose this would be better:

str = malloc(strlen(path->s_name) + 1);

Is that correct?

But I still get crashes.

Thanks,
Mattijs

#32601
Jun 24, 2007 at 9:09pm

str = malloc(strlen(path->s_name) + 1)

is correct and necessary!

But if you’re still getting crashes, then you’re looking at a
different problem. Are you sure that path is a valid t_symbol * (and
not NULL) when you call strlen and strcpy on it?

jb

Am 24.06.2007 um 23:05 schrieb Mattijs Kneppers:

>
> Hi johnpitcairn,
>
> Just a short question: in the other thread you advised me to
> allocate the correct amount of memory for a string copy like this:
>
> str = malloc(strlen(path->s_name));
> if(str != NULL) strcpy(str, path->s_name);
>
> …do stuff…
>
> free(str);
>
> instead of char str[1024];
>
> When I use your method I get occasional crashes when reloading the
> abstraction (oo.call) that contains this external. When I switch
> back to the old method there are no more crashes.
>
> I wonder, is there something obvious I am overlooking? Apart from
> adding one more char for “/0″? I suppose this would be better:
>
> str = malloc(strlen(path->s_name) + 1);
>
> Is that correct?
>
> But I still get crashes.
>
> Thanks,
> Mattijs
> –
> SmadSteck – http://www.smadsteck.nl
> Hard- and software for interactive audiovisual sampling

#107632
Jun 24, 2007 at 9:28pm

Wow, that’s what I call a quick reply! Thanks for the confirmation of the + 1.

path comes all the way from a patcher argument. So it’s not a bad guess that it isn’t correctly initialized prior to this call. In fact the crashes also occur in what from the crash reports seems to be a GUI update (not intiated in my external) and I’d say patcher arguments are the only way to make that happen when I’m not programming a gui object.

It’s strange though that this only happens once in appr. 5 times I save the abstraction that contains the external, maybe something to do with an accidental collision with a screen refresh?

The method in which the strcpy is done is called as a result of a deferred loadbang. I think everything should be properly initialized by then, no?

Thanks,
Mattijs

P.s. one other thing I think I should do is put the “do stuff” inside the if:

str = malloc(strlen(path->s_name) + 1);
if(str != NULL)
{
strcpy(str, path->s_name);

…do stuff…

free(str);
}

I attached some crash reports.

Quote: Jeremy Bernstein wrote on Sun, 24 June 2007 23:09
—————————————————-
> str = malloc(strlen(path->s_name) + 1)
>
> is correct and necessary!
>
> But if you’re still getting crashes, then you’re looking at a
> different problem. Are you sure that path is a valid t_symbol * (and
> not NULL) when you call strlen and strcpy on it?
>
> jb
>
> Am 24.06.2007 um 23:05 schrieb Mattijs Kneppers:
>
> >
> > Hi johnpitcairn,
> >
> > Just a short question: in the other thread you advised me to
> > allocate the correct amount of memory for a string copy like this:
> >
> > str = malloc(strlen(path->s_name));
> > if(str != NULL) strcpy(str, path->s_name);
> >
> > …do stuff…
> >
> > free(str);
> >
> > instead of char str[1024];
> >
> > When I use your method I get occasional crashes when reloading the
> > abstraction (oo.call) that contains this external. When I switch
> > back to the old method there are no more crashes.
> >
> > I wonder, is there something obvious I am overlooking? Apart from
> > adding one more char for “/0″? I suppose this would be better:
> >
> > str = malloc(strlen(path->s_name) + 1);
> >
> > Is that correct?
> >
> > But I still get crashes.
> >
> > Thanks,
> > Mattijs
> > –
> > SmadSteck – http://www.smadsteck.nl
> > Hard- and software for interactive audiovisual sampling
>
>
—————————————————-

#107633
Jun 24, 2007 at 9:33pm

#107634
Jun 24, 2007 at 9:47pm

#107635
Jun 25, 2007 at 12:20am

Apologies to Mattijs for the wild goose chase, I forgot the essential +1 when I made that post…

#107636

You must be logged in to reply to this topic.