Delete Objects Even Without "New"?

VC

    Next

  • 1. Intermittent work in UI thread
    Hi, I'm trying to create a UI thread to do constant background work, but I'm having some design problems. The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will be controlled with thread messages (to start, pause, query...). My problem is where should I put the processing work. I need it to be responsive between cycles, where the process waits for 1 minute, so I can stop it in that period. My only idea is using OnIdle to do the work. But then, how should I make it real idle when waiting for 1 minute? Could anyone give some hints on what is the way to go? Thanks Pedro Ferreira
  • 2. Thread information
    When I use debugger I can see when code is out of a thread by the message (something like) "The thread 0x7676 exited with code 0". Can I know with a similar message the minuet I enter a new thread, and also the thread ID or number? Is there some api to know in any line, which thread I'm in now? Regards Gallia
  • 3. Running on 64-Bit Vista
    I have an old MFC application that installs okay for me on 32-bit Vista. However, a customer is telling me they get the following error when they attempt to install the application on 64-bit Vista: "The version of this file is not compatible with the version of Windows you're running. Check your computer's system information to see whether you need an x86 (32-bit) or x64 (64-bit) version of the program, and then contract the software publisher." I believe the program uses old Wise installer software. Does anyone know what is causing this error and how it could be easily fixed? Thanks! Jonathan
  • 4. problem with CDatabase, CRecordset, stored procedure
    Hi; i have a problem with this sequence 1 CRecordset is used to open a database and retrieve records. 2 Database is closed. 3. PreparedStatement StoredProcedure is executed in same database and works. Output parameters are used. 4. CRecordset is used to open same database and retrieve records 4. fails on .Open() (Unknown ODBC exception) , it works however if the StoredProcedure is not executed. (The StoredProcedure involves complex column binding code to retrieve output columns, as explained on some web sites.). Do you have hints on what to look at. best regards bav

Delete Objects Even Without "New"?

Postby bonmatt10 » Wed, 19 May 2004 09:38:39 GMT

Probably a dumb question, but should I delete pens and rectangles and
device contexts that are created in a way, such as:  CRect RectA ?  If
so, how?

Also, when a window is closed, all its pens, device contexts, etc, are
automatically deleted, right?

Thanks,

Jim

Re: Delete Objects Even Without "New"?

Postby Jerry Coffin » Wed, 19 May 2004 10:44:33 GMT

In article < XXXX@XXXXX.COM >, 
 XXXX@XXXXX.COM  says...

When you create an object (e.g. a CRect) on the stack, you most 
certainly should NOT delete that object.

If you USE that object to create a GDI object (e.g. a DC, as in 
myBMP.CreateCompatibleBitmap) then you need to release that GDI 
object when you're done with it.  GDI objects are always created on 
the GDI heap, so they're always dynamically created and destroyed -- 
there's no direct equivalent of an automatic variable with them.


No.  When/if you end a process, the system will attempt to release 
all the resources owned by that process, but if you just close o 
window it doesn't release any resources except those that are 
associated directly with the window itself -- most of which isn't 
directly visible, such as the message queue.  About the only thing 
that can be released to which you can get more or less direct access 
is the window's DC if its class was registered with the CS_OWNDC 
style.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.

Re: Delete Objects Even Without "New"?

Postby Jerry Coffin » Wed, 19 May 2004 11:29:36 GMT

In article < XXXX@XXXXX.COM >, 
 XXXX@XXXXX.COM  says...

When you create an object (e.g. a CRect) on the stack, you most 
certainly should NOT delete that object.

If you USE that object to create a GDI object (e.g. a BMP, as in 
myBMP.CreateCompatibleBitmap) then you need to release that GDI 
object when you're done with it.  GDI objects are always created on 
the GDI heap, so they're always dynamically created and destroyed -- 
there's no direct equivalent of an automatic variable with them.


No.  When/if you end a process, the system will attempt to release 
all the resources owned by that process, but if you just close o 
window it doesn't release any resources except those that are 
associated directly with the window itself -- most of which isn't 
directly visible, such as the message queue.  About the only thing 
that can be released to which you can get more or less direct access 
is the window's DC if its class was registered with the CS_OWNDC 
style.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.

Re: Delete Objects Even Without "New"?

Postby Joseph M. Newcomer » Wed, 19 May 2004 14:13:28 GMT

CRect RectA allocates a CRect on the stack. It would be meaningless to try to delete it.
Not to mention silly.

Pens, brushes, regions, etc. are a different question, because they have associated kernel
objects, represented by handles. In this case, DeleteObject can be called to explicitly
delete an object. That is usually not necessary, because normally the kernel objects are
implicitly deleted when the destructor for the C++ object is called.

An object cannot be successfully deleted if it is selected into a DC.

Typically, there is no reason to worry about DC existence, because DCs are normally
created as local variables on the stack and implicitly deleted when you leave the context.
It is unbelievably rare to have a DC held longer than the scope of a single function, that
is, the models are

void SomeFunction()
     {
	CClientDC dc(this);
                 	... do stuff here
     }

or more commonly, 

void CClass::OnPaint()
    {
     CPaintDC dc(this);
     ... do drawing
   }

Therefore, the DC has a transient existence and worrying about when it is deleted is
rarely a concern.

Pens are not owned by windows; pens are owned by applications. There is absolutely no
association between a pen and a window, as far as the kernel is concerned.

Normally, you create such associations by creating CPen, CBrush, etc. objects (not CPen *,
mind you, CPen) in the C++ class representing a window. When the C++ objects are
destroyed, their associated kernel objects are destroyed by the destructor of the
containing CWnd-derived object.  This gives the illusion that the pen is owned by the
window, but in fact it is not. The CPen variable is owned by the instance of the
CWnd-derived class, which is a somewhat different question.
				joe







Joseph M. Newcomer [MVP]
email:  XXXX@XXXXX.COM 
Web:  http://www.**--****.com/ 
MVP Tips:  http://www.**--****.com/ 

Similar Threads:

1.Mixing new/delete and operator new/delete?

Is it allowed by the C++ standard to mix the use of 'new/delete' and 
'operator new/delete'?

(a) alloc (version 1) + free (version 2)
(b) alloc (version 2) + free (version 1)


// VERSION 1 ALLOC (using operator new + placement new)
type* data = (type*)operator new(n * sizeof(type));
for (type* p = data; p != data + n; ++p)
    new ((void *)p) value_type();

// VERSION 1 FREE (using calling destructor + operator delete)
for (type* p = data; p != data + n; ++p)
    p->~type();
operator delete(data);

// VERSION 2 ALLOC (using new)
type* data = new type[n];

// VERSION 2 FREE (using delete)
delete [] data;

2.new without delete: what will happen

Hi guys, I guess it is 'not good' if memories allocated by new operator are
not deleted by delete operator. Are there any discussions on how it is not
good? bbg


3.new without delete

The following code for a singleton implementation is recommended in a
website:

class GlobalClass

{

    int m_value;

    static GlobalClass *s_instance;

    GlobalClass(int v = 0)

    {

        m_value = v;

    }

  public:

    int get_value()

    {

        return m_value;

    }

    void set_value(int v)

    {

        m_value = v;

    }

    static GlobalClass *instance()

    {

        if (!s_instance)

          s_instance = new GlobalClass;

        return s_instance;

    }

};



// Allocating and initializing GlobalClass's

// static data member.  The pointer is being

// allocated - not the object inself.

GlobalClass *GlobalClass::s_instance = 0;



void foo(void)

{

  GlobalClass::instance()->set_value(1);

  cout << "foo: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';

}



void bar(void)

{

  GlobalClass::instance()->set_value(2);

  cout << "bar: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';

}



int main(void)

{

  cout << "main: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';

  foo();

  bar();

}


Is the author at fault for the lack of delete statements?  Are there
memory-leak issues?

Thanks a lot,

Paul Epstein

4.Return object without exposing it to delete

Hi all,

I just returned to C++ after programming in other languages, and find
myself a bit puzzled about the following issue: (see attached code).

How can I avoid the deletion of an object that is returned by a method?
Is returning a clone of it the only possibility? That would use a lot of
memory in case of larger objects.


Thanks a lot!

Michael


#include <iostream>
using namespace std;

class Dummy {
public:
    int n;
};

class Victim {
private:
    Dummy *d;

public:
    Victim() {
	d = new Dummy();
	d->n = 23;
    }

    Dummy* getDummy() {
	return d;
    }
};

int main(int argc, char **argv) {
    Victim *v = new Victim();
    Dummy *d = v->getDummy();

    cout << d->n << endl;
    delete d;
    cout << d->n << endl;

    return 0;
}
// EOF

Output:
23
0

5.creating new objects without assignment to a reference

Hello,

  I want to know about "creating new objects without assignment to a
reference" like this :

          ...
          new Employee("John","Woo");
          ....

Is this a good programming practice ? How does garbage collector
handle this issue ?

6. can every object be serialized even with referenced object within in a complex scenario

7. multi-thread new/delete object

8. test to see if object exists with new/delete usage



Return to VC

 

Who is online

Users browsing this forum: No registered users and 66 guest