Jump to page: 1 2 3
Thread overview
Pointers and deleting objects
Sep 26, 2005
Triften Chmil
Sep 26, 2005
Chris Sauls
Sep 26, 2005
Dejan Lekic
Sep 26, 2005
Triften Chmil
Sep 26, 2005
J Thomas
References and deleting objects Clarification
Sep 27, 2005
Triften Chmil
Sep 27, 2005
BCS
Sep 28, 2005
Triften Chmil
Sep 28, 2005
Chris Sauls
Sep 28, 2005
Triften Chmil
Sep 28, 2005
Chris Sauls
Sep 28, 2005
James Dunne
Sep 28, 2005
Regan Heath
Sep 28, 2005
Sean Kelly
Sep 28, 2005
JT
Sep 26, 2005
Mike Parker
Sep 27, 2005
Triften Chmil
Sep 27, 2005
David Medlock
Sep 28, 2005
JT
Sep 28, 2005
JT
September 26, 2005
Is there anything I can do with a pointer to make sure the object it points to hasn't been deleted?

For example:
//We'll assume MyObject has a function/method 'myFunc'
MyObject mo = new MyObject();
MyObject *mop = &mo;
delete(mo);
//What can I check so that this:
mop.myFunc();
//doesn't produce an Access Violation?

i.e.:
if(check something){
mop.myFunc();
}

Thanks much,
Triften Chmil
September 26, 2005
Triften Chmil wrote:
> Is there anything I can do with a pointer to make sure the object it points to
> hasn't been deleted?
> 
> For example:
> //We'll assume MyObject has a function/method 'myFunc'
> MyObject mo = new MyObject();
> MyObject *mop = &mo;
> delete(mo);
> //What can I check so that this:
> mop.myFunc();
> //doesn't produce an Access Violation?
> 

Simple.  Dereference and compare against null.  Example:
# MyObject  mo  = new MyObject;
# MyObject* mop = &mo;
#
# delete mo;
#
# if (*mop !is null)
#   mop.myFunc();

Although I can't help wondering what you need an Object pointer for?  (Its an uncommon thing to see in a language like D.  At least to me.)

-- Chris Sauls
September 26, 2005
If pointers are not for language like D, there would be no pointers there... My opinion on this is - disciplined people, who know how to use pointers, would find it one of the best features in D.

-- 
...........
Dejan Lekic
  http://dejan.lekic.org

September 26, 2005
In article <dh8co6$qb4$1@digitaldaemon.com>, Chris Sauls says...
>
>Triften Chmil wrote:
>> Is there anything I can do with a pointer to make sure the object it points to hasn't been deleted?
>> 
>> For example:
>> //We'll assume MyObject has a function/method 'myFunc'
>> MyObject mo = new MyObject();
>> MyObject *mop = &mo;
>> delete(mo);
>> //What can I check so that this:
>> mop.myFunc();
>> //doesn't produce an Access Violation?
>> 
>
>Simple.  Dereference and compare against null.  Example:
># MyObject  mo  = new MyObject;
># MyObject* mop = &mo;
>#
># delete mo;
>#
># if (*mop !is null)
>#   mop.myFunc();
>
>Although I can't help wondering what you need an Object pointer for?  (Its an
> uncommon thing to see in a language like D.  At least to me.)
>
>-- Chris Sauls

I'm programming a shoot-em-up video game and a weapon that locks-on to an enemy keeps a pointer to that enemy and so can reference the enemy's location to track it. I want to make sure the enemy hasn't been destroyed before I try to lookup its locations.

Unless my understanding of programming terms and methods is horribly off, passing a copy of the enemy object wouldn't do, because the location values would change, so you keep a pointer to the enemy object. I suppose you might use someother locating technique (an index in an array) which would basically still be a pointer.

If I'm missing some particular aspect of the nature of D, please let me know. (I.E. pointers are evil, or some such.)

Thanks again,
Triften Chmil
September 26, 2005
yah: class references are basically pointers in D

MyObject mo = new MyObject();

mo is a pointer, MyObject* is redundant and only really usefull in more obscure circumstances




Triften Chmil wrote:
> In article <dh8co6$qb4$1@digitaldaemon.com>, Chris Sauls says...
> 
>>Triften Chmil wrote:
>>
>>>Is there anything I can do with a pointer to make sure the object it points to hasn't been deleted?
>>>
>>>For example:
>>>//We'll assume MyObject has a function/method 'myFunc'
>>>MyObject mo = new MyObject();
>>>MyObject *mop = &mo;
>>>delete(mo);
>>>//What can I check so that this:
>>>mop.myFunc();
>>>//doesn't produce an Access Violation?
>>>
>>
>>Simple.  Dereference and compare against null.  Example:
>># MyObject  mo  = new MyObject;
>># MyObject* mop = &mo;
>>#
>># delete mo;
>>#
>># if (*mop !is null)
>>#   mop.myFunc();
>>
>>Although I can't help wondering what you need an Object pointer for?  (Its an
>>uncommon thing to see in a language like D.  At least to me.)
>>
>>-- Chris Sauls
> 
> 
> I'm programming a shoot-em-up video game and a weapon that locks-on to an enemy
> keeps a pointer to that enemy and so can reference the enemy's location to track
> it. I want to make sure the enemy hasn't been destroyed before I try to lookup
> its locations.
> 
> Unless my understanding of programming terms and methods is horribly off,
> passing a copy of the enemy object wouldn't do, because the location values
> would change, so you keep a pointer to the enemy object. I suppose you might use
> someother locating technique (an index in an array) which would basically still
> be a pointer.
> 
> If I'm missing some particular aspect of the nature of D, please let me know.
> (I.E. pointers are evil, or some such.)
> 
> Thanks again,
> Triften Chmil
September 26, 2005
Triften Chmil wrote:

> If I'm missing some particular aspect of the nature of D, please let me know.
> (I.E. pointers are evil, or some such.)

You shouldn't need to use pointers with class instances in D as, unlike C++, they are references. The only cases in D where you even need to think about pointers, off the top of my head, are when interfacing with C code, fetching values from associative arrays with the 'in' operator, and in some cases perhaps when working with struct instances as they are not references in D (though most of the time you would want a struct pointer you can use inout parameters instead).
September 27, 2005
Oh, okay. That would explain some the stranger behavior I've been trying to work around.

Thanks.

In article <dh97rk$1kdn$1@digitaldaemon.com>, Mike Parker says...
>
>Triften Chmil wrote:
>
>> If I'm missing some particular aspect of the nature of D, please let me know. (I.E. pointers are evil, or some such.)
>
>You shouldn't need to use pointers with class instances in D as, unlike C++, they are references. The only cases in D where you even need to think about pointers, off the top of my head, are when interfacing with C code, fetching values from associative arrays with the 'in' operator, and in some cases perhaps when working with struct instances as they are not references in D (though most of the time you would want a struct pointer you can use inout parameters instead).


September 27, 2005
So now I've basically got:

MyObject mo = new MyObject();
MyObject mo2 = mo;
//mo2 is a reference to mo, right?

delete(mo);
//this seems to cause no trouble
int x = mo2.myvar;
mo2.myFunc();

I'd like to be able to check for deletion of the object referenced by mo using mo2. Is this possible?

In article <dh8qjd$164t$1@digitaldaemon.com>, J Thomas says...
>
>yah: class references are basically pointers in D
>
>MyObject mo = new MyObject();
>
>mo is a pointer, MyObject* is redundant and only really usefull in more obscure circumstances
>
>
>
>
>Triften Chmil wrote:
>> In article <dh8co6$qb4$1@digitaldaemon.com>, Chris Sauls says...
>> 
>>>Triften Chmil wrote:
>>>
>>>>Is there anything I can do with a pointer to make sure the object it points to hasn't been deleted?
>>>>
>>>>For example:
>>>>//We'll assume MyObject has a function/method 'myFunc'
>>>>MyObject mo = new MyObject();
>>>>MyObject *mop = &mo;
>>>>delete(mo);
>>>>//What can I check so that this:
>>>>mop.myFunc();
>>>>//doesn't produce an Access Violation?
>>>>
>>>
>>>Simple.  Dereference and compare against null.  Example:
>>># MyObject  mo  = new MyObject;
>>># MyObject* mop = &mo;
>>>#
>>># delete mo;
>>>#
>>># if (*mop !is null)
>>>#   mop.myFunc();
>>>
>>>Although I can't help wondering what you need an Object pointer for?  (Its an uncommon thing to see in a language like D.  At least to me.)
>>>
>>>-- Chris Sauls
>> 
>> 
>> I'm programming a shoot-em-up video game and a weapon that locks-on to an enemy keeps a pointer to that enemy and so can reference the enemy's location to track it. I want to make sure the enemy hasn't been destroyed before I try to lookup its locations.
>> 
>> Unless my understanding of programming terms and methods is horribly off, passing a copy of the enemy object wouldn't do, because the location values would change, so you keep a pointer to the enemy object. I suppose you might use someother locating technique (an index in an array) which would basically still be a pointer.
>> 
>> If I'm missing some particular aspect of the nature of D, please let me know. (I.E. pointers are evil, or some such.)
>> 
>> Thanks again,
>> Triften Chmil


September 27, 2005
Triften Chmil wrote:
> In article <dh8co6$qb4$1@digitaldaemon.com>, Chris Sauls says...
> 
>>Triften Chmil wrote:
>>
>>>Is there anything I can do with a pointer to make sure the object it points to hasn't been deleted?
>>>
>>>For example:
>>>//We'll assume MyObject has a function/method 'myFunc'
>>>MyObject mo = new MyObject();
>>>MyObject *mop = &mo;
>>>delete(mo);
>>>//What can I check so that this:
>>>mop.myFunc();
>>>//doesn't produce an Access Violation?
>>>
>>
>>Simple.  Dereference and compare against null.  Example:
>># MyObject  mo  = new MyObject;
>># MyObject* mop = &mo;
>>#
>># delete mo;
>>#
>># if (*mop !is null)
>>#   mop.myFunc();
>>
>>Although I can't help wondering what you need an Object pointer for?  (Its an
>>uncommon thing to see in a language like D.  At least to me.)
>>
>>-- Chris Sauls
> 
> 
> I'm programming a shoot-em-up video game and a weapon that locks-on to an enemy
> keeps a pointer to that enemy and so can reference the enemy's location to track
> it. I want to make sure the enemy hasn't been destroyed before I try to lookup
> its locations.
> 
> Unless my understanding of programming terms and methods is horribly off,
> passing a copy of the enemy object wouldn't do, because the location values
> would change, so you keep a pointer to the enemy object. I suppose you might use
> someother locating technique (an index in an array) which would basically still
> be a pointer.
> 
> If I'm missing some particular aspect of the nature of D, please let me know.
> (I.E. pointers are evil, or some such.)
> 
> Thanks again,
> Triften Chmil


Why are you deleting the object?
D's garbage collector is fairly good, and I haven't seen signifigant performance issues with it thus far(I do OpenGL coding too).

(Sorry if I may not be understanding your intention/approach correctly.)

-DavidM
September 27, 2005
"Triften Chmil" <Triften_member@pathlink.com> wrote in message news:dhaf74$2jkn$1@digitaldaemon.com...
> So now I've basically got:
>
> MyObject mo = new MyObject();
> MyObject mo2 = mo;
> //mo2 is a reference to mo, right?
>
> delete(mo);
> //this seems to cause no trouble
> int x = mo2.myvar;
> mo2.myFunc();
>
> I'd like to be able to check for deletion of the object referenced by mo
> using
> mo2. Is this possible?

Nope.  Unless the GC were to keep a list of pointers to .. uhh, pointers that pointed to the objects, the GC has no way of setting mo2 to any value that says "this is an invalid object" (i.e. null).  mo gets set to null as the parameter to delete is actually an inout parameter (hence why you can't use an expression as the parameter to delete).

This situation wouldn't really come up that often though.  Usually if you're deleting an object, you will get rid of all references to it, as all references to it will then be invalid.


« First   ‹ Prev
1 2 3