Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Gaiza

macrumors newbie
Original poster
Jun 11, 2009
2
0
Hello, i want to use c++ in my iphone app (it's my main language & i can port code on other systems)

So i started a C++ class, with an NSThread as member.
Added some functions :

void WaitFoThreadToFinish();
void StartThread();
void StopThread();
static void ThreadProc(void* pOwner);

the first point is my function StartThread(), wich is implemented like this:

Code:
void CSimpleClass::StartThread()
{
    if (m_pTimerThread != nil)
	{
        [m_pTimerThread cancel];
        WaitFoThreadToFinish();
    }
    
    NSThread *driverThread = [[NSThread alloc] initWithTarget:nil selector:@selector(TimerThredProc:) object:(NSObject*)this];
    m_pTimerThread = driverThread;
    [driverThread release];
    
    [m_pTimerThread start];
}

actually i pass nil as initWithTarget, i should use "this", but i get compiler error. if i set "self" i get compiler error... (not declared in this scope)

As i am e newbee with OBC, can someone explain me how to do this ?

sorry for my bad english

Thanks.
 

Menge

macrumors 6502a
Dec 22, 2008
611
3
Amsterdam
I don't know if you can call Objective-C code from C/C++ code.

I think you can only go from Objective-C to C/C++ and not the other way around.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
If you're using C++ for cross platform, why not use pthreads directly? NSThread's initWithTarget:selector:eek:bject: method takes an id for object, so if you're passing a C++ object then that will fail as NSThread might be doing a retain on the object.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Hello, i want to use c++ in my iphone app (it's my main language & i can port code on other systems)

So i started a C++ class, with an NSThread as member.
Added some functions :

void WaitFoThreadToFinish();
void StartThread();
void StopThread();
static void ThreadProc(void* pOwner);

the first point is my function StartThread(), wich is implemented like this:

Code:
void CSimpleClass::StartThread()
{
    if (m_pTimerThread != nil)
	{
        [m_pTimerThread cancel];
        WaitFoThreadToFinish();
    }
    
    NSThread *driverThread = [[NSThread alloc] initWithTarget:nil selector:@selector(TimerThredProc:) object:(NSObject*)this];
    m_pTimerThread = driverThread;
    [driverThread release];
    
    [m_pTimerThread start];
}

actually i pass nil as initWithTarget, i should use "this", but i get compiler error. if i set "self" i get compiler error... (not declared in this scope)

As i am e newbee with OBC, can someone explain me how to do this ?

You are trying to mix C++ and Objective C.

First thing you have to do is save the file with the extension ".mm" which tells the compiler that it is a mix of C++ and Objective-C; without that it will complain bitterly about anything that looks like Objective-C.

Next, just for a bit of precision: Your C++ objects can't have an NSThread as a member, only an NSThread*. Different thing. Unlike C++, in Objective-C all objects are dynamically allocated.

Now where the problem is: When you initialise an NSThread object, you need to pass pointers to Objective-C objects and an Objective-C selector. C++ objects are not Objective-C objects, and casting pointers doesn't turn them into Objective-C objects. If you insist on mixing Objective-C and C++, you'd have to start with Objective-C objects. An Objective-C object can contain a pointer to a C++ object and call C++ methods.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I also noticed your memory management is wrong. You create the thread, assign it to an ivar, then release the thread, but you don't retain the ivar, so that thread just deallocated and when you go to start it'll crash because the pointer wasn't reassigned to nil. You need something like:
Code:
m_pTimerThread = [driverThread retain];
 

Gaiza

macrumors newbie
Original poster
Jun 11, 2009
2
0
So as i understand, i have to use obc as a wrapper to my c++ class, and use stl, boost or whatever into them...

Thanks a lot for your answer
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.