std.stream.File help required (and classes)
Ali Çehreli
acehreli at yahoo.com
Tue Mar 27 15:46:26 PDT 2012
On 03/27/2012 02:57 PM, akaz wrote:
> B) In my function:
> private int msf_open(MSFilter* f, void* arg){ //a setter
> printf("msf_binfile_open-start\n============================\n");
> MSF_State* s=cast(MSF_State*)f.data;
> ms_mutex_lock(&(f.lock));
> s.filedesc = new File(*(cast(string*)arg),"w+b");
Judging from your free(f.data) proposal below, I think the problem is on
the previous line: the object is allocated in GC's memory but s.filedesc
is in a memory that GC is not scanning. You must call GC.addRange() when
you allocate the memory that s.filedesc is living on.
> can you tell me if the line s.filedesc=new
> File(*(cast(string*)arg),"w+b"); PROPAGATES the change into f.data?
Yes. It is the same as in C.
> (recall that MSF_State* s=cast(MSF_State*)f.data;). I should also force,
> at the end of the function (just before return), something like:
> free(f.data); f.data=s; to propagate this?
Not to propagate, but you may have to call free() to avoid a leak.
> As dumb as it may seem: is my "s" variable in MSF_State*
> s=cast(MSF_State*)f.data; a POINTER or a VALUE?
A pointer just like in C.
>I should write
> s->filedesc? Or, maybe, (*s).filedesc?
That syntax has been useless even in C. (Although it communicates
something to the reader.) The compiler could use the dot operator and do
the right thing depending on whether the left-hand side was a pointer or
an object.
D does not have the -> operator.
> I am a bit lost between pointers
> (s->x or (*s).x) and values (s.x). For structures there are pointers,
> for classes there are no pointers?
Yes.
Ali
More information about the Digitalmars-d-learn
mailing list