![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
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;
}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.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
But this isn't C, or C++. Its Objective C
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
Apparently dragon master doesn't know what he is talking about.
|
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
Quote:
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.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#7 | ||
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
Quote:
And please be kind to people in the forum if you want them to be kind to you. Quote:
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) |
||
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#9 | ||
|
Programmer
|
Quote:
Quote:
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 |
||
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
How ignorant of you(Dragon_Master). Not every one is going to use microsoft API because it is crap
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|