www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Need help with simple event framework

reply Tano Sakaria <tanojoshu yahoo.de> writes:
Hello guys,

I need some help regarding a (very) basic Event handling framework I
just started on today.
Please note that I'm still fairly new to programming itself and that
I might not know things that seem obvious to others.

The attached file is my work so far. I have a class called Backbone
which houses 2 pointers for a linked list. Every linked list member
contains 2 private pointers (one forward and one backward), the
associated public functions to acces them, a string for the Event_ID
and a function pointer to the function that shall be called, if an
event with the corresponding Event_ID is invoked.

I also have a simple test class with exactly one function to test
whether the function is indeed called.



I can compile without problems, but I get a runtime error "Access
Violation: Read at address 0x4E4". This runtime error gets thrown
once I call the function getNext() through a pointer, but I honestly
have no idea why.

I am using D1 and Tango, though Tango is only for the line printing
and can easily be converted to Phobos.

I hope you can help me!
Nov 19 2011
parent reply Kagamin <spam here.lot> writes:
You escape pointer to stack allocated object.
Nov 19 2011
parent reply Tano Sakaria <tanojoshu yahoo.de> writes:
Why can't I do that?

What can I do to circumvent this?
Nov 19 2011
parent reply Jacob Carlborg <doob me.com> writes:
On 2011-11-19 21:51, Tano Sakaria wrote:
 Why can't I do that?

 What can I do to circumvent this?

Allocate the object on the heap using "new". -- /Jacob Carlborg
Nov 19 2011
parent reply Tano Sakaria <tanojoshu yahoo.de> writes:
I'm sorry, but I don't understand.

How can I use "new" in conjunction with a pointer? "new" creates an
object, but don't I need a pointer to said object instead for my
linked list?
Nov 19 2011
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/19/2011 01:22 PM, Tano Sakaria wrote:
 I'm sorry, but I don't understand.

 How can I use "new" in conjunction with a pointer? "new" creates an
 object, but don't I need a pointer to said object instead for my
 linked list?

Without seeing the original code, foo() below returns a pointer to an object allocated on the heap: struct S {} S * foo() { S * p = new S; return p; } void main() { S * p = foo(); } That object belongs to the D runtime and may be destroyed at an unspecified time when there are no more references to it. Ali
Nov 19 2011
parent reply Tano Sakaria <tanojoshu yahoo.de> writes:
When I do that, I get an error "Cannot implicitly convert type
HandlerLink to HandlerLink*".

By the way, how can I split up my classes into seperate source files?
Just copying the code into one file per class and changing the imports
doesn't work. I tried compiling the classes seperately, into libraries
and what not, but all I get is Linker-errors as soon as compiling the
Backbone class.
Setting the -I flag for the import path does not work, either.
Nov 20 2011
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
I could not figure out how to see Backbone.d, so I cannot see your code.

On 11/20/2011 01:52 AM, Tano Sakaria wrote:
 When I do that, I get an error "Cannot implicitly convert type
 HandlerLink to HandlerLink*".

That error must have been changed since D1. I get a similar one when I return a pointer to a 'class' variable as opposed to a 'struct' variable. If you are really using a class, then you don't need a pointer: class S {} S foo() { /* * The left hand side is a class variable, while the * anonymous object that has been created with new on the * right hand side is a clas object. Class objects are * accessed by class variables. * * They are different things. They are confusing because * the object is defined as 'class S{}' earlier and * its reference variable is defined as 'S var'. (I think * this is the same in Java and perhaps C#.) * * Returning the class variable from the function is fine * because the actual object lives in the heap. */ S var = new S; return var; } void main() { S var = foo(); } Please provide a smaller code in plain text. (Some people put their code on a clipboard-like code site. You can do that too if the code is too long.)
 By the way, how can I split up my classes into seperate source files?

(Note: It would be better if this question had its separate thread.) The following is on Linux. I think the object file extensions are .obj on Windows.
 Just copying the code into one file per class and changing the imports
 doesn't work.

You must give those files to the compiler as well. Assuming the program has the main module (deneme.d) and two others: $ dmd deneme.d hayvan/kedi.d hayvan/kopek.d
 I tried compiling the classes seperately,

That works too. Assuming that the two modules have been compiled with the -c flag to generate their .o files: $ dmd deneme.d hayvan/kedi.o hayvan/kopek.o
 into libraries

That works too. First make a library that consists of the modules: $ dmd hayvan/kedi.d hayvan/kopek.d -lib -ofhayvan Then use that library when building the program: $ dmd deneme.d hayvan.a Most of the above are from one of the not-yet-translated chapters of D.ershane: http://ddili.org/ders/d/moduller.html
 and what not, but all I get is Linker-errors as soon as compiling the
 Backbone class.
 Setting the -I flag for the import path does not work, either.

-I is for the location of the import files; it doesn't involve compilation. Ali
Nov 20 2011
parent Tano Sakaria <tanojoshu yahoo.de> writes:
Thanks a lot, it works now!
Nov 20 2011