Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'this' not volatile?

Tags:

c++

symbian

Having spent the last few days debugging a multi-threading where one thread was deleting an object still in use by another I realised that the issue would have been far easier and quicker to diagnose if I could have made 'this' volatile. It would have changed the crash dump on the system (Symbian OS) to something far more informative.

So, is there any reason why it cannot be, or shouldn't be?

Edit: So there really is no safe way to prevent or check for this scenario. Would it be correct to say that one solution to the accessing of stale class pointers is to have a global variable that holds the pointer, and any functions that are called should be statics that use the global variable as a replacement for 'this'?

static TAny* gGlobalPointer = NULL;

#define Harness static_cast<CSomeClass*>(gGlobalPointer);

class CSomeClass : public CBase
    {
public:
    static void DoSomething();

private:
    int iMember;
    };


void CSomeClass::DoSomething()
    {
    if (!Harness)
        {
        return;
        }

    Harness->iMember = 0;
    }

So if another thread deleted and NULLed the global pointer it would be caught immediately.

One issue I think with this is that if the compiler cached the value of Harness instead of checking it each time it's used.

like image 239
James Avatar asked Jul 01 '11 09:07

James


People also ask

What does it mean by non-volatile?

1. Volatile substances have a tendency to vaporize whereas nonvolatile substances do not have a tendency to vaporize. 2. Volatile substances have a high vapor pressure at normal room temperature and pressure. Nonvolatile substances do not have a high vapor pressure in these conditions.

Why is non-volatile?

Non-volatile memory is memory that retains its values even when power is removed. Earlier forms of non-volatile memory included various forms of read-only memory (ROM).

Which one is not volatile?

non-volatile memory include read-only memory (see ROM), flash memory, most types of magnetic computer storage devices (e.g. hard disks, floppy discs and magnetic tape), optical discs, and early computer storage methods such as paper tape and punched cards.

Which is not volatile memory?

The most common example of non-volatile memory is ROM (Read Only Memory). Non-volatile memory affects a system's capacity to a large extent. It is a type of digital memory that does not lose any content with an interruption of the power supply. One does not need to refresh it periodically.


2 Answers

this is not a variable, but a constant. You can change the object referenced by this, but you can't change the value of this. Because constants never change, there is no need to mark them as volatile.

like image 174
phlogratos Avatar answered Sep 18 '22 02:09

phlogratos


It wouldn't help: making a variable volatile means that the compiler will make sure it reads its value from memory each time it's accessed, but the value of this doesn't change, even if, from a different context or the same, you delete the object it's pointing at.

like image 38
Andrew Aylett Avatar answered Sep 17 '22 02:09

Andrew Aylett