Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 12th, 2006, 12:03 PM   #1
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Objective C Memory Management problems

I am trying to create a program that is intended to be a bank simulation. So, I made an account class which will hold each account, and a bank class, which is supposed to represent the main bank.

I have attached the files below. please have a look at them, they are not very long.

My problem is that at my main I want to find a way to make a loop which will insert accounts into the bank. I could do this with a while statement, but I chose the 'for' loop for testing purposes.

Can you tell me how am I going to do this and be 'memory management-friendly'? I have managed to insert the values into the bank, but not also release the objects properly!

#include <iostream>
#include <string>
#import <Foundation/Foundation.h>
#import "Account.h"
#import "bank.h"
#include "functions.mm"

#define AAI(x) Account *x = [[Account alloc] init];

using std::cin;
using std::cout;
using std::string;

Bank *myBank = [Bank alloc];

void addAccToBank(){
	string s;
	NSString *str;
	Account *acc = [[Account alloc] init];
	[acc autorelease];
	int i;
	int deposit;
	for(i=0; i<2; i++){
		
		cout << "Give a name: ";
		getline(cin, s);
		cout << "Give a deposit: ";
		cin >> deposit;
		cin.get();
		
		str = [[NSString alloc] initWithCString: s.c_str() encoding: NSASCIIStringEncoding];
		[acc setName: str andDeposit: deposit];
		
		[myBank addToBank: acc];
		
		cout << [acc retainCount] << " is the retain count\n";
	}
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	[myBank initialize];
	
	addAccToBank();
	
	[myBank printAllEntries];
	[pool release];
    return 0;
}
I have ommited the release commands. Note that the result is wrong! Both accounts on the bank have the same values, even if I give them different ones! Can you give me a solution?

1)When I add an object to an NSArray, isn't that element deeply copied into the NSArray? Why does this happen?
2)How can I fix the problems I am having with my program?

These memory management problems are driving me nuts. Any help will be greatly appreciated.
Attached Files
File Type: zip ccc.zip (3.9 KB, 10 views)
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Jun 26th, 2006, 1:19 PM   #2
Dragon_Master
Programmer
 
Dragon_Master's Avatar
 
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3 Dragon_Master is on a distinguished road
Try using GlobalAlloc. It's a windows API function (include windows.h). Um, besides that, I's like to mention, that you're using C++ not C, C doesn't have classes.(it's just a matter of sematics, ignore what I said) C and C++ are different. C++ has OOP. C doesn't. C++ has classes. C doesn't.
Dragon_Master is offline   Reply With Quote
Old Jun 26th, 2006, 1:44 PM   #3
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
But this isn't C, or C++. Its Objective C
Wizard1988 is offline   Reply With Quote
Old Jun 26th, 2006, 2:29 PM   #4
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
Apparently dragon master doesn't know what he is talking about.
andro is offline   Reply With Quote
Old Jun 26th, 2006, 3:41 PM   #5
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Quote:
Try using GlobalAlloc. It's a windows API function (include windows.h). Um, besides that, I's like to mention, that you're using C++ not C, C doesn't have classes.(it's just a matter of sematics, ignore what I said) C and C++ are different. C++ has OOP. C doesn't. C++ has classes. C doesn't.
Dude, what are you talking about? Are you sure you are using OS X (as your profile says)?

The code I presented is Objective-C++, not plain C++.

Anyway, I fixed the problem long ago, and for everyone that's interested on how to solve this problem, here is the code for it: (as you can see, I am porting the same program to different languages just for exercising )
This time I used plain Objective C, as Obj C++ caused me problems, and also I had no particular reason of using it.
Attached Files
File Type: zip source.zip (4.7 KB, 13 views)
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Jun 26th, 2006, 4:26 PM   #6
Dragon_Master
Programmer
 
Dragon_Master's Avatar
 
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3 Dragon_Master is on a distinguished road
I use mac, linux and windows. I program primary for windows (because of it's popularity, not because I like it). I know what objective C is, so shut up. The topic should then be objective C++, not object C. That's what meant.
Dragon_Master is offline   Reply With Quote
Old Jun 26th, 2006, 4:39 PM   #7
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Quote:
I know what objective C is, so shut up. The topic should then be objective C++, not object C. That's what meant.
No you don't. And that's not what you meant. If it was, then I'm not the only one here who got you wrong, so it was your fault, not mine.

And please be kind to people in the forum if you want them to be kind to you.

Quote:
Um, besides that, I's like to mention, that you're using C++ not C, C doesn't have classes.(it's just a matter of sematics, ignore what I said) C and C++ are different.
Here, it is clear that you meant PLAIN C++ and not Objective C++. You obviously have no idea what you were talking about. Objective C has classes, in fact, the whole concept of ObjC is built around them. So, you meant PLAIN C and PLAIN C++. If you didn't then you would have mentioned it.

And the reason that I asked you "Are you sure you are using OS X (as your profile says)?" is to verify that you are using OS X and NOT OS 9 (which didn't have ObjC so it would be natural to confuse my first post).

I suggest you read the 'How to Post' threads in this forum. Do not insult other people, especially if they have not insulted you.

PS: If you wanted to talk about ObjC - ObjC++, why did you recommend me a windows-only function that wouldn't work with it?
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Jun 26th, 2006, 9:29 PM   #8
Dragon_Master
Programmer
 
Dragon_Master's Avatar
 
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3 Dragon_Master is on a distinguished road
So what? I don't program in Objective C. And Mac is a piece of crap, BTW. I don't like the API in mac.
Dragon_Master is offline   Reply With Quote
Old Jun 26th, 2006, 11:46 PM   #9
gryfang
Programmer
 
gryfang's Avatar
 
Join Date: May 2006
Location: Ohio
Posts: 36
Rep Power: 0 gryfang is on a distinguished road
Send a message via AIM to gryfang
Quote:
Originally Posted by Dragon_Master
...And Mac is a piece of crap, ...
Quote:
Originally Posted by Wizard1988
...microsoft API because it is crap.
Yay aren't opinions loveley. :banana: They hold sooooo much ground in an arguement.

You know What Soul I have to say never heard of Objective C so I was confused at first, but seeing that it seems to be more of a MAC X thing that could explain it (I only have OS 8.6). Could you give a link of a good tutorial or references for me. You've spiked my interest.
__________________
--------------------
LOAD "*" ,8,1

God bless
- Gryfang
gryfang is offline   Reply With Quote
Old Jun 26th, 2006, 9:36 PM   #10
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
How ignorant of you(Dragon_Master). Not every one is going to use microsoft API because it is crap
Wizard1988 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC