Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Programming Languages (http://www.programmingforums.org/forum38.html)
-   -   Objective C Memory Management problems (http://www.programmingforums.org/showthread.php?t=10302)

Soulstorm Jun 12th, 2006 12:03 PM

Objective C Memory Management problems
 
1 Attachment(s)
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.

Dragon_Master Jun 26th, 2006 1:19 PM

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.

Wizard1988 Jun 26th, 2006 1:44 PM

But this isn't C, or C++. Its Objective C

andro Jun 26th, 2006 2:29 PM

Apparently dragon master doesn't know what he is talking about.

Soulstorm Jun 26th, 2006 3:41 PM

1 Attachment(s)
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? :confused: 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.

Dragon_Master Jun 26th, 2006 4:26 PM

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.

Soulstorm Jun 26th, 2006 4:39 PM

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?

Dragon_Master Jun 26th, 2006 9:29 PM

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.

Wizard1988 Jun 26th, 2006 9:36 PM

How ignorant of you(Dragon_Master). Not every one is going to use microsoft API because it is crap :rolleyes:

gryfang Jun 26th, 2006 11:46 PM

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.


All times are GMT -5. The time now is 8:05 AM.

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