CocoaDev

Edit AllPages

I have just started Cocoa development using XCode 2.1. My application is to load big binary files (signals from a scientific research tool) and process the data and eventually show some images. I’m currently using Cocoa document based setting with Objective-C. I am observing quite a mysterious phenomenon.

I created myDocument class derived from NSDocument. It mallocs a bunch of SInt16, UInt32, and float arrays necessary for the processing. All of them are freed after processing. The end results are displayed as an instance of myImage class that derived from CIImage (I’m planning to use core image filtering later on). In Debug mode, my code works perfectly for the first time only. Whenever I open 2nd or 3rd files, the output looks weird as if it reads totally different location of the memory. In my understanding, with the document based application, one file is assigned to one instance of myDocument class. And any new files should be assigned to newly allocated instances so that there should not be any memory overlapping. Is my assumption correct?

Furthermore, when I launch from MallocDebug, my code works perfectly!!! I can open as many files as I want without having any problem. Why is that?


Please PostYourCode.


myDocument class

currentImage is referred from a subclass of NSWindowController and passed it to a subclass of NSView so that the associated window can show the generated image.

Once again, whenever I launch this application from MallocDebug, it works fine and I can open as many files as I want without any problem.

But if I simply run the executable without using MallocDebug, the first file is displayed properly. But from the 2nd file, it looks like as if the software is reading totally different memory space…


Maybe you are trying to do too much at once. Why don’t you try letting the readFromFile do its one job and then trigger the prepareGlobalArray and processBinaryData with another control?

Or, for a novel idea, use the debugger. ;-) Set a breakpoint in the beginning of the readFromFile method and step through it as it opens the first, then again as it opens the next. You’d be surprised what the debugger can tell you.


It doesn’t exactly help much, but I’ve removed the tabs from your source code and swapped mallocs for (more correct?) callocs.

I’m a little concerned by the line rawData[index+y] = (UInt32)(abs(logf(A.realp[y]A.realp[y]+…, since index doesn’t appear to be initialized (or, indeed, declared). Did you mean xpixelsY here perhaps? That’d cause the problem for sure.

Try running lint over your code, and turning all warnings on (-Wall -Wpedantic is it?).


Thanks for your input. I accidentally erased the line initializing “index” when I copied the code. Yes, it is properly initialized.

As an interlim solution, I will create a new class just to handle binary files and try to create an instance of this class after myDocument completes readFromFile function. I will report the result tomorrow.

Fix the code above, perhaps? We can’t help you any further if we know the code isn’t correct :)


OK, I separated the data processing from myDocument class. Now myDocument is simply initialize myWindow by passing the file path to be processed. After myWindow awakeFromNib, it initializes myData with the file path. All the processing is done in myData and CIImage object is passed to myView on myWindow. Now everything is working beautifully.

So what I found was that NSDocument class is initialized for each TYPE of document and is not necessarily spawned for each DOCUMENT users open. On the other hand, NSWindow class is initialized every time NSDocument opens a file. This is why my initial code did not work after first file was opened. Strange thing is that MallocDebug seems to force NSDocument to spawn new instance every time a file opened. I still wonder why…

Mm, doesn’t sound right. Each document is indeed a different object.

Yeah, I won’t dispute the fact that you fixed it, but your reasons are utterly bogus.


*So what I found was that NSDocument class is initialized for each TYPE of document and is not necessarily spawned for each DOCUMENT users open. *

This is untrue. A new instance of your NSDocument class is instantiated for every document in your application.

Well, not exactly. An instance of NSDocument *can be reinitialized (in the English sense, not the Cocoa sense) by reloading the data from a read method (like readFromFile:ofType:). –JediKnil**

Having said that, I don’t know what your problem is, and I won’t know until I see your entire project, along with your source data.

It’s completely appropriate to do this kind of model crunching in your NSDocument subclass if that class is the entirety of your model data. In this case, it looks like that is true. In larger applications, NSDocument is merely a controller class that routes data between model objects and view objects. In your case a model object would be a new Objective-C class that is responsible for decyphering your binary data and converting it to different forms. C.f., NSImage for an example. Doing number-cruncing in view objects, such as your NSWindow instance, is a terrible practice and will limit the reusability of your code.

Enjoy! – MikeTrent


I see something strange in the lines

        sourceFile = fileName copy] autorelease];

        documentType = [[docType copy] autorelease];

I wonder about their use in what follows, vis-a-vis the [[NSFileHandle myData.