Pointer deletion question

M

Marcus

Hi, when I try and delete a pointer in another class I get out of
memory errors. Why does this fail? Is it not ok to do this?

A.cpp
void A::foo(){
new MyObject *pObj = new MyObject;
B b;
b.foo2(pObj);
}
///
B.cpp
void B:foo2(MyObject *pObj){
//do something
delete pObj;
}
 
J

John Harrison

Marcus said:
Hi, when I try and delete a pointer in another class I get out of
memory errors. Why does this fail? Is it not ok to do this?

A.cpp
void A::foo(){
new MyObject *pObj = new MyObject;
B b;
b.foo2(pObj);
}
///
B.cpp
void B:foo2(MyObject *pObj){
//do something
delete pObj;
}

It's legal to do it, although it's not usually considered good style.
But it fails for you because you have a bug somewhere in your program.

As is commonly the case the bug is in the code you didn't post. See
Thomas Tutone's post in the thread 'Multiple substrings = Error' for a
very good explanation of how to post programming problems (and look at
the rest of the thread for how much time gets wasted when you don't
follow these guidelines).

john
 
M

Marcus

It's legal to do it, although it's not usually considered good style.
But it fails for you because you have a bug somewhere in your program.

As is commonly the case the bug is in the code you didn't post. See
Thomas Tutone's post in the thread 'Multiple substrings = Error' for a
very good explanation of how to post programming problems (and look at
the rest of the thread for how much time gets wasted when you don't
follow these guidelines).

john- Hide quoted text -

- Show quoted text -

Sorry if you misunderstood... my intention wasn't to have the group
debug my code, I was just wondering if it was legal to do that. Now
that I know that it's ok, I can look elsewhere for my problem. Thank
you for the information, very much appreciated.
 
?

=?iso-8859-1?Q?Thomas_H=FChn?=

Marcus said:
A.cpp
void A::foo(){
new MyObject *pObj = new MyObject;
^^^
Is this "new" a typo or have you used it deliberately?

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top